复杂 Json 到嵌套 POJO spring MVC

Complex Json to Nested POJO spring MVC

我正在尝试使用 @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 {}。我在这里错过了什么?

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

例如:

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

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

PropertyNamingStrategy.SnakeCaseStrategy

工作代码(已测试)。

Getter 和 setter 对此很重要。但是@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);
    }

}