我正在使用 intelliJIDEA,这是我的 Controller :
@RequestMapping(value = "/api/authors", method = RequestMethod.POST)
public AuthorDTO addNewAuthor(@RequestBody AuthorDTO authorDTO) {
return authorService.add(authorDTO);
}
( authorService.add
返回 AuthorDTO
类型。)
作者DTD.java:
public class AuthorDTO {
public AuthorDTO() {
}
public AuthorDTO(Author author) {
this.id = author.getId();
this.first_name = author.getFirstName();
this.last_name = author.getLastName();
}
public AuthorDTO(Long id, String first_name, String last_name) {
this.id = id;
this.first_name = first_name;
this.last_name = last_name;
}
private Long id;
private String first_name;
private String last_name;
//getter/setters
}
这是我的休息测试窗口:
但是当我发送POST
时请求,没有任何反应!
已发送 json:{"id":"12"}, {"first_name":"aaaa"}, { "last_name": "gggg"}
输出:
响应窗口:<Response body is empty>
运行日志:
2016-01-13 09:40:18.206 INFO 3892 --- [nio-8082-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-01-13 09:40:18.206 INFO 3892 --- [nio-8082-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2016-01-13 09:40:18.338 INFO 3892 --- [nio-8082-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 132 ms
最佳答案
检查一下JSON的格式,应该是,
{"id":12, "first_name":"aaaa", "last_name": "gggg"}
确保Content-Type
header 设置为application/json
关于java - 向 spring Rest api 发送 post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34759633/