简体   繁体   中英

Json response in Spring Boot

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.

did you try

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

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

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