【发布时间】:2023-04-02 07:10:01
【问题描述】:
我想发送以下 POST 请求:
POST: /api/test
{
"words": ["running", "testing", "and"]
}
我的控制器如下所示:
@RequestMapping(value={"/api/test"}, method=RequestMethod.POST)
@ResponseBody
public ResponseEntity<Text> getWords(@RequestBody Words words) {
// Do something with words...
return new ResponseEntity<Text>(new Text("test"), HttpStatus.OK);
}
单词类:
public class Words {
private List<String> words;
public Words(List<String> words) {
this.words = words;
}
public List<String> getWords() {
return words;
}
}
发送字符串而不是列表时,它可以正常工作,但是使用列表时出现以下错误:
Could not read document:
No suitable constructor found for type [simple type, class api.models.tokenizer.Words]: can not instantiate from JSON object (need to add/enable type information?)\n at [Source: java.io.PushbackInputStream@60f2a08; line: 2, column: 5]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class api.models.tokenizer.Words]: can not instantiate from JSON object (need to add/enable type information?)\n at [Source: java.io.PushbackInputStream@60f2a08; line: 2, column: 5]
【问题讨论】:
标签: java spring jackson spring-boot