Jackson无法使用单参数构造函数构造实例。

huangapple go评论77阅读模式
英文:

Jackson cannot construct instance with one parameter constructor

问题

我正在使用Spring Boot创建一个Web应用程序其中一个端点期望一个具有一个属性的JSON对象```studentId```。我像我的其他函数一样使用DTO来捕获有效载荷

```java
@PostMapping("/courses/{id}/students")
public SuccessResponse<Void> addEnrolls(@PathVariable Long id, @RequestBody StudentIdPayload payload) throws HandledException {
    courseService.addEnrolls(id, payload.getStudentId());
    return success(HttpStatus.OK);
}
@Data
@AllArgsConstructor
public class StudentIdPayload {
    private Long studentId;
}

但是,当我尝试使用JSON主体{"studentId":1}发布端点时,我收到以下错误:

com.fasterxml.jackson.databind.exc.MismatchedInputException: 无法构造`org.bimoadityar.univms.dto.input.StudentIdPayload`的实例(尽管至少存在一个创建者):无法从对象值反序列化(无委托或基于属性的创建者)

而如果我只使用值1进行发布,则可以正常工作。

如何使它能够处理对象有效载荷?

有趣的是,当我向StudentIdPayload添加另一个属性,比如String placeholder时,它按预期工作,尽管这个解决方案看起来有些不正规。


<details>
<summary>英文:</summary>

I am using Spring Boot to create a web application. One of the endpoints expect a json object having one property, i.e. ```studentId```. I am using DTO like my other functions to capture the payload.

```java
@PostMapping(&quot;/courses/{id}/students&quot;)
public SuccessResponse&lt;Void&gt; addEnrolls(@PathVariable Long id, @RequestBody StudentIdPayload payload) throws HandledException {
    courseService.addEnrolls(id, payload.getStudentId());
    return success(HttpStatus.OK);
}
@Data
@AllArgsConstructor
public class StudentIdPayload {
    private Long studentId;
}

But when I tried to post the endpoint with json body {&quot;studentId&quot;:1}, I got the following error :

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `org.bimoadityar.univms.dto.input.StudentIdPayload` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)

While it works if I post using just the value 1.

How can I get it to work with the object payload?

Interestingly, when I add another property to the StudentIdPayload, such as String placeholder, it works as intended, although this solution feels hacky.

答案1

得分: 1

考虑到 https://github.com/FasterXML/jackson-databind/issues/1498,似乎这是预期的行为。

对于我的特定情况,我满意地向我的构造函数添加了 @JsonCreator

@Data
@AllArgsConstructor(onConstructor = @__(@JsonCreator))
public class StudentIdPayload {
    private Long studentId;
}
英文:

Considering https://github.com/FasterXML/jackson-databind/issues/1498, it seems that this is the intended behavior.

For my particular case, I am satisfied with adding the @JsonCreator to my constructor.

@Data
@AllArgsConstructor(onConstructor = @__(@JsonCreator))
public class StudentIdPayload {
    private Long studentId;
}

答案2

得分: -2

默认情况下,反序列化需要无参构造函数,因此添加 @NoArgsConstructor

@Data
@AllArgsConstructor
@NoArgsConstructor
public class StudentIdPayload {
    private Long studentId;
}

另请参阅:

英文:

By default, deserialization requires no-args constructor, so add @NoArgsConstructor:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class StudentIdPayload {
    private Long studentId;
}

see also:

huangapple
  • 本文由 发表于 2020年6月29日 09:20:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/62629941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定