英文:
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("/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;
}
But when I tried to post the endpoint with json body {"studentId":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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论