复杂的Json到嵌套的POJO spring MVC

时间:2018-02-14 06:57:45

标签: java json spring-mvc pojo

我正在尝试使用bot.delete_webhook()

将以下Json导入POJOS
@RequestBody Instance instance

以下是我的POJO

实例

{
  "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"
  }
}

参数

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; } 。有没有办法将下划线分隔的json密钥放入java的变量命名约定(Camelcase)??

我尝试将@JsonProperty用于我的POJO类而不是每个参数的@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)。我只是在@JsonProperty中得到一个空的json {}。我在这里缺少什么?

1 个答案:

答案 0 :(得分:1)

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

例如:

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

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

  

PropertyNamingStrategy.SnakeCaseStrategy

工作代码(TESTED)。

吸气剂和制定者对于这项工作非常重要。但@JsonProperty不需要它们

User.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;
    }

}

Role.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