简体   繁体   中英

Complex Json to Nested POJO spring MVC

I am trying to get the following Json into POJOS using @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"
  }
}

Below are my POJOs

Instance

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

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

I use @JsonProperty everywhere. Is there any way to get underscore separated json keys into java's naming convention for variables (Camelcase)??

I tried using @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) to my POJO classes instead of the @JsonProperty for each parameter. I just get an empty json {} in instance . What am I missing here?

Yes, is this possible using PropertyNamingStrategy class through JsonNaming annotation

Ex:

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

//---- The below code has updated. In that code am using

PropertyNamingStrategy.SnakeCaseStrategy

Working code (TESTED).

Getters and setters are important for this to work. But @JsonProperty does not require them

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

}

Here is the Controller

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

}

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM