代码之家  ›  专栏  ›  技术社区  ›  Sam Thomas

到嵌套POJO spring MVC的复杂Json

  •  0
  • Sam Thomas  · 技术社区  · 7 年前

    我正在尝试使用 @RequestBody Instance instance

    {
      "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)中??

    我试过使用 @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) 我的POJO课程,而不是 @JsonProperty公司 对于每个参数。我只得到一个空的json {} 在里面 instance . 我错过了什么?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Sankar    7 年前

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

    前任:

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

    //---- 以下代码已更新。在我使用的代码中

    物业管理策略。蛇案战略

    工作代码(已测试)。

    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);
        }
    
    }
    

    enter image description here