【问题标题】:Complex Json to Nested POJO spring MVC复杂 Json 到嵌套 POJO spring MVC
【发布时间】:2018-02-14 06:57:44
【问题描述】:

我正在尝试使用 @RequestBody Instance instance 将以下 Json 导入 POJOS

{
  "service_id": "service-id-here",
  "plan_id": "plan-id-here",
  "context": {
    "platform": "cloudfoundry",
    "some_field": "some-contextual-data"
  },
  "organization_guid": "org-guid-here",
  "space_guid": "space-guid-here",
  "parameters": {
    "agent_name": 1,
    "url": "foo",
    "credential": "asdasd",
    "ia_url": "asdasd"
  }
}

以下是我的 POJO

实例

public class Instance {
    @JsonProperty(value = "service_id")
    String serviceId;
    @JsonProperty(value = "plan_id")
    String planId;
    //TODO : Replace with Context class when the spec defines things clearly
    @JsonProperty(value = "context")
    Object context;
    @JsonProperty(value = "organization_guid")
    String organizationGuid;
    @JsonProperty(value = "space_guid")
    String spaceGuid;
    @JsonProperty(value = "parameters")
    Parameters parameters;
}

参数

    public class Parameters {
        @JsonProperty(value = "agent_name")
        String agentName;
        @JsonProperty(value = "url")
        String url;
        @JsonProperty(value = "credential")
        String credential;
        @JsonProperty(value = "ia_url")
        String iaUrl;
}

我在任何地方都使用@JsonProperty。有没有办法将下划线分隔的json键放入java的变量命名约定(Camelcase)??

我尝试在我的 POJO 类中使用 @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class),而不是为每个参数使用 @JsonProperty。我只是在instance 中得到一个空的json {}。我在这里错过了什么?

【问题讨论】:

    标签: java json spring-mvc pojo


    【解决方案1】:

    是的,这是否可以通过 JsonNaming 注释使用 PropertyNamingStrategy 类

    例如:

    @JsonNaming(PropertyNamingStartergy.LowerCaseWithUnderscoresStrategy.class)
    class Class_name{
      ...
    }
    

    //---- 以下代码已更新。在那个代码中我正在使用

    PropertyNamingStrategy.SnakeCaseStrategy

    工作代码(已测试)。

    Getter 和 setter 对此很重要。但是@JsonProperty 不需要它们

    用户.java

    @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
    public class User {
        private int id;
        private String beanName;
        private Role role;
    
    
        public Role getRole() {
            return role;
        }
        public void setRole(Role role) {
            this.role = role;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getBeanName() {
            return beanName;
        }
        public void setBeanName(String beanName) {
            this.beanName = beanName;
        }
    
    }
    

    角色.java

    @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
    public class Role {
        private int id;
        private String roleName;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getRoleName() {
            return roleName;
        }
        public void setRoleName(String roleName) {
            this.roleName = roleName;
        }
    
    }
    

    这里是控制器

    @RestController
    @RequestMapping("/test")
    public class NamingController {
    
        @RequestMapping(value="/jsontopojo", method = RequestMethod.POST)
        public ResponseEntity<User> jsontopojo(@RequestBody User nam) {
            return new ResponseEntity<User>( nam, HttpStatus.OK);
        }
    
    }
    

    【讨论】:

    • 已经由@TaherKhorshidi 建议...也阅读我的评论
    • 哦..好的!我已经通过我推荐给你的 sn-p 解决了。
    • 是的,但您推荐的命名策略已被弃用,取而代之的是 SnakeCase,正如问题中所述,它对我不起作用
    • 好的。尝试在你的 POJO 类中实现 Serializable 接口
    • 确保 POJO 字段和 JSON 字段应该相同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 2022-12-31
    • 2021-12-15
    • 2018-03-08
    • 2014-04-08
    • 2013-09-21
    • 2020-03-19
    相关资源
    最近更新 更多