Complex Json to Nested POJO spring MVC

Sam Thomas Source

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


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;

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?



answered 6 months ago Sankar #1

Yes, is this possible using PropertyNamingStrategy class through JsonNaming annotation


class Class_name{

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


Working code (TESTED).

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

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) { = id;
    public String getBeanName() {
        return beanName;
    public void setBeanName(String beanName) {
        this.beanName = beanName;


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


Here is the Controller

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

comments powered by Disqus