【问题标题】:Spring Boot Automatic JSON to Object at ControllerSpring Boot 自动 JSON 到控制器的对象
【发布时间】:2017-03-07 22:49:03
【问题描述】:

我有具有该依赖项的 SpringBoot 应用程序:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

我的控制器有一个方法如下:

@RequestMapping(value = "/liamo", method = RequestMethod.POST)
@ResponseBody
public XResponse liamo(XRequest xRequest) {
    ...
    return something;
}

我通过 AJAX 从我的 HTML 发送一个带有 XRequest 类型对象字段的 JSON 对象(它是一个没有任何注释的普通 POJO)。但是我的 JSON 没有在我的控制器方法中构造成对象,并且它的字段为空。

我错过了控制器上的自动反序列化功能?

【问题讨论】:

  • 可能是@RequestBody 参数上的@RequestBody 注释:stackoverflow.com/questions/11291933/… - 顺便说一句,如果您将 spring-mvc 用于控制器,则不需要 spring-boot-starter-jersey
  • 您缺少 @RequestBody 就像 @zapl 注意到的那样,如果您要发送 JSON,我还会将 consumes = "application/json" 添加到 @RequestMapping
  • 答案是这样。缺少 RequestBody,谢谢!你能把它写成接受它的答案吗?

标签: java json spring-mvc spring-boot jackson


【解决方案1】:

Spring boot 带有开箱即用的 Jackson,它将负责将 JSON 请求正文解组到 Java 对象

您可以使用 @RequestBody Spring MVC 注解将 JSON 字符串反序列化/解组为 Java 对象...例如。

示例

@RestController
public class CustomerController {
    //@Autowired CustomerService customerService;

    @RequestMapping(path="/customers", method= RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public Customer postCustomer(@RequestBody Customer customer){
        //return customerService.createCustomer(customer);
    }
}

使用@JsonProperty 用相应的json 字段名称注释您的实体成员元素。

public class Customer {
    @JsonProperty("customer_id")
    private long customerId;
    @JsonProperty("first_name")
    private String firstName;
    @JsonProperty("last_name")
    private String lastName;
    @JsonProperty("town")
    private String town;
}

【讨论】:

  • 很抱歉。该问题本身现在处于已删除状态。删除链接,无论如何复制粘贴大部分内容。感谢您指出。
  • createCustomer() 在这种情况下看起来如何?这个答案很有帮助,除了我的 create() 方法需要一堆字符串而不是一个对象
  • 它是如何映射的?我的意思是如果两个属性具有相同的名称。
  • @ravi 当您尝试启动应用程序时它会出错。话虽这么说,你自己试过吗?
  • 如果你使用的是SpringBoot,也可以使用这个SpringBoot solution
【解决方案2】:

SpringBoot 默认自带这个功能。您只需在控制器方法的参数声明中使用 @RequestBody 注释,但与 @so-random-dudeanswer 相比,您不必使用 @JsonProperty 注释字段,这不是必需的。

您只需为您的自定义 XML 对象类提供 getter 和 setter。为了简单起见,我在下面发布了一个示例。

示例:

控制器方法声明:-

@PostMapping("/create")
    public ResponseEntity<ApplicationResponse> createNewPost(@RequestBody CreatePostRequestDto createPostRequest){
        //do stuff
        return response;
    }

您的自定义 XML 对象类:-

public class CreatePostRequestDto {
    String postPath;

    String postTitle;

    public String getPostPath() {
        return postPath;
    }

    public void setPostPath(String postPath) {
        this.postPath = postPath;
    }

    public String getPostTitle() {
        return postTitle;
    }

    public void setPostTitle(String postTitle) {
        this.postTitle = postTitle;
    }
}

【讨论】:

    猜你喜欢
    • 2020-10-10
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 2015-06-10
    • 2020-10-19
    相关资源
    最近更新 更多