在 Spring Rest Controller 中出现 JSON 解析错误 (MismatchedInputException)
Getting JSON parsing error (MismatchedInputException) in Spring Rest Controller
我对 Spring 很陌生。当我在 Postman 中尝试 POST 方法时出现 JSON 解析错误。基本上,我有一个 class 我想以列表的形式调用另一个。
我有一个用于 classes 的抽象实体,然后我有一个标签 class;
@Entity
@Data
@EqualsAndHashCode(callSuper = true)
public class Tag extends AbstractEntity {
@Column (nullable = false)
private String tag;
我有一个问题class:
@Entity
@Data
@EqualsAndHashCode(callSuper = true)
public class Question extends AbstractEntity {
@Column (nullable = false)
private String title;
@Column (nullable = false)
private String content;
@OneToMany
@Column (nullable = false)
private List<Tag> tag;
这是我的控制器:
@RestController
@RequestMapping("v1/enquiry")
public class EnquiryController {
@Autowired
private QuestionRepository questionRepository;
@PostMapping
public ResponseEntity<Question> createEnquiry(@RequestBody Question question) {
if (question.getTitle() == null | question.getContent() == null) {
throw new BadRequest("Please fill in the required fields!");
}
Question enq = questionRepository.save(question);
return ResponseEntity.ok().body(enq);
}
当我使用 POST 方法时:
{
"title": "question",
"content": "cogito",
"tag": ["java", "rest"]
}
我收到以下错误。我已经尝试了所有针对类似条件的建议。 None 他们成功了。我做错了什么?
"message": "JSON parse error: Cannot construct instance of com.mockup.mockupapi.model.Tag
(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('java'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of com.mockup.mockupapi.model.Tag
(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('java')\n at [Source: (PushbackInputStream); line: 4, column: 10] (through reference chain: com.mockup.mockupapi.model.Question[\"tag\"]->java.util.ArrayList[0])",
SOLVED
感谢评论中的线索,我更改了 JSON 中的数组格式,并将 CascadeType
添加到 OneToMany
调用中。我使用 .PERSIST
,但是,.ALL
也可以。
JSON格式:
{
"title": "question",
"content": "cogito",
"tag": [{"tag":"java"},{"tag": "rest"}]
}
问题class:
@Entity
@Data
@EqualsAndHashCode(callSuper = true)
public class Question extends AbstractEntity {
@Column (nullable = false)
private String title;
@Column (nullable = false)
private String content;
@OneToMany(cascade = CascadeType.PERSIST)
@Column (nullable = false)
private List<Tag> tag;
请更改您的 JSON 结构。
{ "title": "question", "content": "cogito", "tag": [{"tag":"java"},{"tag": "rest"}] }
同时在您的映射中添加级联。
@OneToMany (cascade = CascadeType.ALL)
@Column (nullable = false)
private List<Tag> tag;
我对 Spring 很陌生。当我在 Postman 中尝试 POST 方法时出现 JSON 解析错误。基本上,我有一个 class 我想以列表的形式调用另一个。
我有一个用于 classes 的抽象实体,然后我有一个标签 class;
@Entity
@Data
@EqualsAndHashCode(callSuper = true)
public class Tag extends AbstractEntity {
@Column (nullable = false)
private String tag;
我有一个问题class:
@Entity
@Data
@EqualsAndHashCode(callSuper = true)
public class Question extends AbstractEntity {
@Column (nullable = false)
private String title;
@Column (nullable = false)
private String content;
@OneToMany
@Column (nullable = false)
private List<Tag> tag;
这是我的控制器:
@RestController
@RequestMapping("v1/enquiry")
public class EnquiryController {
@Autowired
private QuestionRepository questionRepository;
@PostMapping
public ResponseEntity<Question> createEnquiry(@RequestBody Question question) {
if (question.getTitle() == null | question.getContent() == null) {
throw new BadRequest("Please fill in the required fields!");
}
Question enq = questionRepository.save(question);
return ResponseEntity.ok().body(enq);
}
当我使用 POST 方法时:
{
"title": "question",
"content": "cogito",
"tag": ["java", "rest"]
}
我收到以下错误。我已经尝试了所有针对类似条件的建议。 None 他们成功了。我做错了什么?
"message": "JSON parse error: Cannot construct instance of
com.mockup.mockupapi.model.Tag
(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('java'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance ofcom.mockup.mockupapi.model.Tag
(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('java')\n at [Source: (PushbackInputStream); line: 4, column: 10] (through reference chain: com.mockup.mockupapi.model.Question[\"tag\"]->java.util.ArrayList[0])",
SOLVED
感谢评论中的线索,我更改了 JSON 中的数组格式,并将 CascadeType
添加到 OneToMany
调用中。我使用 .PERSIST
,但是,.ALL
也可以。
JSON格式:
{
"title": "question",
"content": "cogito",
"tag": [{"tag":"java"},{"tag": "rest"}]
}
问题class:
@Entity
@Data
@EqualsAndHashCode(callSuper = true)
public class Question extends AbstractEntity {
@Column (nullable = false)
private String title;
@Column (nullable = false)
private String content;
@OneToMany(cascade = CascadeType.PERSIST)
@Column (nullable = false)
private List<Tag> tag;
请更改您的 JSON 结构。
{ "title": "question", "content": "cogito", "tag": [{"tag":"java"},{"tag": "rest"}] }
同时在您的映射中添加级联。
@OneToMany (cascade = CascadeType.ALL)
@Column (nullable = false)
private List<Tag> tag;