找不到接口java.util.List Rest API Spring boot的主要或默认构造函数

时间:2019-02-13 05:45:57

标签: java spring-boot postman

我正在将请求主体传递给邮递员的POST请求,类似于:

 "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方法如下所示。

  @RequestMapping("/create")
  public Planet create(@RequestBody Planet data) {
      Planet mars = planetService.create(data.getName(),data.getArtifacts());
      return mars;

Planet及其所有嵌套对象都具有默认构造函数,例如:

public Planet() {}

但是,由于缺少默认构造函数,我无法创建新的Planet对象。请帮忙!

编辑: 行星类

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
}
}

4 个答案:

答案 0 :(得分:1)

我不明白您面临的问题是什么,但是我可以立即看到一个错误,因此我猜测这就是您面临的问题,我将为您提供解决方案。

创建一个与您的json数据结构相匹配的类,如下所示:

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注释来接收完整的请求正文。

@RequestMapping("/create")
  public String create(@RequestBody PlanetData data) {
      Planet mars = planetService.create(data.getName(),data.getArtifacts());
      return mars.toString();
  }

编辑:查看Planet类,还需要进行一些修改

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.

}

希望这可以解决您的问题。

答案 1 :(得分:1)

当我忘记参数前的@RequestBody时遇到了错误

  @RequestMapping("/create")
  public Planet create(@RequestBody Planet data) {

答案 2 :(得分:0)

您应该这样写:

...
public String create(@RequestBody JSONObject requestParams) {
      String name=requestParams.getString("name");
      List<Planet> planetArtifacts=requestParams.getJSONArray("artifacts").toJavaList(Planet.Class);
...

答案 3 :(得分:0)

我想,它正在尝试调用没有构造函数的new List()。尝试在签名中使用new ArrayList<>()

如果以此方式工作,则您已找到错误。然后重新考虑调用方法的概念,因为您通常希望避免在方法签名中使用List的实现