发送请求到spring rest api

时间:2016-01-13 06:14:07

标签: java spring rest intellij-idea

我正在使用intelliJIDEA,这是我的控制器:

@RequestMapping(value = "/api/authors", method = RequestMethod.POST)
public AuthorDTO addNewAuthor(@RequestBody AuthorDTO authorDTO) {
    return authorService.add(authorDTO);
}

authorService.add返回AuthorDTO类型。)

AuthorDTD.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
 }

这是我的休息测试窗口:

enter image description here

但是当我发送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

1 个答案:

答案 0 :(得分:2)

检查JSON的格式,它应该是,

{"id":12, "first_name":"aaaa",  "last_name": "gggg"}

确保Content-Type标头设置为application/json