0

I am generating Rest Services using Jackson. I am able to get JSON response like

[{"userName":"scott","userId":7},{"userName":"toe","userId":101}]

but i am expecting response with Pojo name pointing to the response like below

{"UserDetails":[{"userName":"scott","userId":7},{"userName":"toe","userId":101}]}

Here is my implementation class

@Service("userServices")
public class UserServiceImpl implements UserServices{

private UserServices userServices;

public UserServices getUserServices() {
    return userServices;
}
public void setUserServices(UserServices userServices) {
    this.userServices = userServices;
}

@Override
public List<UserDetails> getUser(UserDetails userDetails) {


    ObjectMapper mapper = new ObjectMapper();

    List<UserDetails> user = new ArrayList<UserDetails>();
    List<UserDetails> list = new ArrayList<UserDetails>();

    UserDetails userDetails2 = new UserDetails();
    userDetails2.setUserName("scott");
    userDetails2.setUserId(007);

    UserDetails userDetails3 = new UserDetails();
    userDetails3.setUserName("toe");
    userDetails3.setUserId(101);

    user.add(userDetails2);
    user.add(userDetails3);

    try{
    String jsonString = mapper.writeValueAsString(user);
    UserDetails[] userDetails4 = mapper.readValue(jsonString, UserDetails[].class);
    list = Arrays.asList(userDetails4);
    System.out.println(userDetails4);
    }catch (Exception e) {
        System.out.println(e);
    }
    return list;
}
}

and here is my pojo

public class UserDetails implements Serializable{

private String userName;
private int userId;

public UserDetails(){

}

 //getters and setters...
}

** Note:** I don't want to use any annotations at the domain object side.

usm426
  • 15
  • 5

3 Answers3

1

Just return

UserDetailsResponse:

public class UserDetailsResponse {

    private List<UserDetails> userDetails;
    // getter and setter

}

instead of list of UserDetails in the UserServiceImpl (and presumably then in the controller).

Pistolnikus
  • 161
  • 1
  • 5
  • It's working fine but I think it's not applicable to multiple service call methods. any suggestion for this – usm426 Sep 16 '19 at 08:41
  • not sure if I understand your problem correctly, but you can keep the service to return List (as you had) and then do the mapping to response object only when it leaves your application (e.g. in controller) – Pistolnikus Sep 16 '19 at 09:59
0

You return a List of UserDetails which translates to your response object being a JSON Array. In order to get your desired result you would need to create a Container class (UserDetailsContainer) which has the property userDetails of the List type and return an instance of that as your response.

js758
  • 71
  • 4
0

You can use objectMapper.writeValueAsString(aCustomMap) for custom json view creation on the fly.

Example:

UserDetails userDetails2 = new UserDetails();
userDetails2.setUserName("scott");
userDetails2.setUserId(007);

UserDetails userDetails3 = new UserDetails();
userDetails3.setUserName("toe");
userDetails3.setUserId(101);

Map<String, List<UserDetails>> m = new HashMap<>();
m.put("UserDetails", Arrays.asList(userDetails2, userDetails3));

System.out.print(
    //This is what you need
    objectMapper.writeValueAsString(m)
);
//{"UserDetails":[{"userName":"scott","userId":7},{"userName":"toe","userId":101}]}
lprakashv
  • 1,121
  • 10
  • 19
  • thanks for the reply, when i use this i am getting compilation exception like Type mismatch: cannot convert from Map> to List – usm426 Sep 16 '19 at 07:42
  • Sorry, replace the Car with your UserDetails class name. Tested a template code with a Car class. – lprakashv Sep 16 '19 at 08:12
  • yeah I already changed that even though it's giving same error – usm426 Sep 16 '19 at 08:39
  • What is your updated code? My code is getting compiled and working. It was just a demonstration to use a map for the desired result. You have to use it according to your code requirement. Looks like you are passing this map where a List is required. Please use the map to generate the final JSON/JSON-string and not make is unnecessarily everywhere. – lprakashv Sep 16 '19 at 08:45
  • Map> m = new HashMap<>(); m.put("UserDetails", Arrays.asList(userDetails2, userDetails3)); try { System.out.print( //This is what you need mapper.writeValueAsString(m) ); } catch (JsonProcessingException e) { e.printStackTrace(); } here i want to return the list of user details object – usm426 Sep 16 '19 at 08:52
  • Change the method signature from List to Map>. Otherewise, it will return a list and in the final json, you cannot get the name tag "UserDetails". – lprakashv Sep 16 '19 at 08:58
  • Thanks for the solution it's working fine, Is there any other approach without any changes to method return type – usm426 Sep 16 '19 at 09:55
  • You create the map in the controller method and not in the service method. Service method should return the list, controller method should take the decision of how to show it to the user in the API. – lprakashv Sep 17 '19 at 03:55