【问题标题】:SpringBoot JACKSON configuration for JSON nested objects received by Spring MVCSpring MVC 接收的 JSON 嵌套对象的 Spring Boot JACKSON 配置
【发布时间】:2016-03-01 16:23:02
【问题描述】:

我已经创建了 Spring Boot 应用程序,在这个应用程序中我有

@RestController
public class OfferController {

    @RequestMapping(value = "/saveOffer", method = RequestMethod.POST)
    public void saveOffer(@RequestBody Offer offer) {
    //...
    }
}

Offer 类包含 Address 类型的嵌套属性

public class Offer {

   private String title;
   private Address address;

   //... getters setters etc
}

当我从 UI 发送 JSON

{
  "offer": {
    "title":"TheBestOffer",
    "address": {
      "city": "Warsaw"
    }
  }
}

我的 REST 控制器收到 Offer,Address 属性为空,但 title 属性包含值 "TheBestOffer"(发送时)。

我假设 JACKSON 随 Spring Boot 一起交付需要对嵌套对象进行一些额外配置?我试过这样做,但没有用:/

【问题讨论】:

    标签: spring-mvc spring-boot jackson


    【解决方案1】:

    Spring 会自动执行此操作,我认为您的问题在于 json。

    您需要删除优惠标签。

    {
        "title":"TheBestOffer",
        "address": {
          "city": "Warsaw"
        }
    }
    

    【讨论】:

    • 但是为什么它会填充 title 属性呢?
    • 我不确定为什么要设置 title 属性(我做了一些测试并没有设置值),但这是发送请求正文的正确方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    相关资源
    最近更新 更多