- @RequestBody标注在List作为参数,请求方式
curl -H “Content-type: application/json” -XPOST -d ‘[{“name”:“ThreeBody”, “id”:1}]’ “http://localhost:9111/test/path”
@RequestMapping(value = "/test/path", method = RequestMethod.POST)
public String insertBooks(@RequestBody List<Book> bookList) {
return "";
}
class Book {
private long id;
private String name;
/** com.fasterxml.jackson反序列化时需要一个无参构造方法 */
public Book() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
注意这里Book需要一个无参的构造函数,否则会出现类似如下的异常 com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of Book
(although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)