Json response in Spring Boot

时间:2018-02-03 08:56:00

标签: spring-boot jackson jsonresponse

I am using spring boot, while returning the object as JSON response I am getting something like this below.

{
  "userId": "e340f903-033f-45ba-91c0-798678a71510",
  "firstName": "Varun",
  "lastName": "Sabkanln",
  "reputationCount": 0
}

what I want, is a key assigned to this response.

{ 
 "profile":{
            "userId": "e340f903-033f-45ba-91c0-798678a71510",
            "firstName": "Varun",
            "lastName": "Sabkanln",
             "reputationCount": 0
           }
}

This class is as follows.

public class UserProfileDao {

    private String userId;

    private String firstName;

    private String lastName;

    private int reputationCount;
 }

Is there any jackson annotation for this or any other simpler way with less code.

2 个答案:

答案 0 :(得分:0)

did you try

public class profile {
    private UserProfileDao userProfileDao;
    //some getters and some setters.
}

答案 1 :(得分:0)

Create another POJO like this and return an object of this class

public class Profile {
    private UserProfileDao profile;
    //getters and setters.
}

Other way is to use @JsonRootName annotation

@JsonRootName(value = "profile")
public class UserProfileDao {

    private String userId;

    private String firstName;

    private String lastName;

    private int reputationCount;
 }