java - 向 spring Rest api 发送 post 请求

标签 java spring rest intellij-idea

我正在使用 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
 }

这是我的休息测试窗口:

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

最佳答案

检查一下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/

相关文章:

java - 字符的拆分和求和

java - 除了不可变值对象之外,我什么时候应该覆盖 `equals()` ?

Java 返回退出代码 1

rest - 向 EC2 实例上运行的 ElasticSearch 服务发送请求

java - 仅适用于 S3 的自定义 aws 包装 jar

facebook - 使用 Facebook OAuth 保护 Rest API 端点的可能方法

java - 指定 Google Caliper 基准的附加维度

spring - 重载发布请求映射

java - 如何使用 Spring 表达式语言(或您知道的其他表达式语言)为定义为变量的一些对象列表赋值

java - 我应该如何在 Spring boot 中配置 httpMessageConverters 以将消息转换为所需的格式?