No primary or default constructor found for interface java.util.List Rest API Spring boot(未找到接口 java.util.List Rest API Spring boot 的主构造函数或默认构造函数)
问题描述
我正在将请求正文传递给邮递员的 POST 请求,类似于:
I am passing a request body to a POST request on postman similar to this:
"name":"Mars",
"artifacts":[
{
"elements":[
{
"name":"carbon",
"amount":0.5,
"measurement":"g"
}
],
"typeName":"typeA"
},
{
"elements":[
{
"name":"hydrogen",
"amount":0.2,
"measurement":"g"
}
],
"typeName":"typeB"
}
]
rest 控制器中的 create 方法如下所示.
The create method in the rest controller looks like this.
@RequestMapping("/create")
public Planet create(@RequestBody Planet data) {
Planet mars = planetService.create(data.getName(),data.getArtifacts());
return mars;
Planet 及其所有嵌套对象都有一个默认构造函数,例如:
Planet and all its nested objects have a default constructor such as:
public Planet() {}
但是,由于缺少默认构造函数,我无法创建新的行星对象.请帮忙!
However, I am not able to create a new planet object because of lack of a default constructor. Please help!
行星类
public class Planet {
@JsonProperty("name")
private String name;
@Field("artifacts")
private List<Artifact> artifacts;
public Planet() {}
public Planet(String name, List<Artifact> artifacts)
{
this.name = name;
this.artifacts = artifacts;
}
//setters and getters
}
工件类:
public class Artifact() {
@Field("elements")
private List<Element> elements;
@JsonProperty("typeName")
private String typeName;
public Artifact() {}
public Artifact(String typeName, List<Element> elements)
{
this.typeName = typeName;
this.elements = elements;
}
}
元素类:
public class Element() {
@JsonProperty("elementName")
private String name;
@JsonProperty("amount")
private double amount;
@JsonProperty("measurement")
private String measurement;
public Element() {}
public Element(String name, double amount, String measurement)
{
//assignments
}
}
推荐答案
我不明白你所面临的问题是什么,但我可以立即看到一个错误,所以猜测这就是你所面临的问题,我是会给你一个解决方案.
I don't understand what is the issue you are facing, but i can see an error straight away so guessing that is the issue you are facing, i am going to give you a solution.
创建一个与您的 json 数据结构匹配的类,如下所示:
Create a class which matches your json data structure like this :
Class PlanetData {
private String name;
private List<Planet> artifacts;
public PlanetData(String name, List<Planet> artifacts){
name = name;
artifacts = artifacts;
}
// include rest of getters and setters here.
}
那么你的控制器应该是这样的.基本上,您需要将 @RequestBody
放入要从请求 JSON 接收的所有参数中.之前您只将 @RequestBody
放入名称参数而不是工件参数,并且由于请求正文只能使用一次,因此您需要一个包装类来使用单个 @RequestBody接收完整的请求正文代码>注释.
Then your controller should look like this. Basically you needed to put @RequestBody
to all the parameters you want to recieve from request JSON. Earlier you only put @RequestBody
to name parameter not artifact parameter and since Request Body can be consumed only once, so you need a wrapper class to recieve the complete request body using single @RequestBody
annotation.
@RequestMapping("/create")
public String create(@RequestBody PlanetData data) {
Planet mars = planetService.create(data.getName(),data.getArtifacts());
return mars.toString();
}
查看 Planet 类,它还需要一些修改
Edit : Looking at the Planet class, it also needs some modification
public class Planet {
private String typeName; // key in json should match variable name for proper deserialization or you need to use some jackson annotation to map your json key to your variable name.
private List<Element> elements;
public Planet() {}
public Planet(String typeName, List<Element> elements)
{
this.typeName = typeName;
this.elements = elements;
}
//setters and getters. Remember to change your setters and getter from name to typeName.
}
希望这能解决您的问题.
Hope this solves your issue.
这篇关于未找到接口 java.util.List Rest API Spring boot 的主构造函数或默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:未找到接口 java.util.List Rest API Spring boot 的主构造函数或默认构造函数
基础教程推荐
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01