2

I am designing a web service using Jersey and I need to add some custom fields to my JSON response always, similar to this:

{
    "result": "0"/"Error code",
    "message": "Message returned by server",
    "custom_field": here goes string or class values required
}

For example, I have Token and User POJO classes:

public class Token {
    private String token = null;

    public Token (String token) {
        this.token = token;
    }

    // getter and setter
}

public class User {
    private String id= null;
    private String name = null;
    private String surname = null;
    private String phone = null;

    public User (String id, String name, String surname, String phone) {
        this.id = id;
        this.name = name;
        this.surname = surname;
        this.phone = phone;
    }

    // getters and setters
}

And this are login and getUser webservices:

@Path("/services")
@Produces(MediaType.APPLICATION_JSON)
public class HelloFromCxfRestService {

    @POST
    @Path("/login")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Token login(String login, String password){
        //Logic
        return token;
    }

    @POST
    @Path("/get_user")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String getUser (String id){
        //Logic
        return user; //in custom_field would be {"name":"myName", "surname":"mySurname", "phone":"myPhone"}
    }
}

How can I return my custom JSON structure respecting POJOs? Obviously I should can put custom values in "result" and "message".

Angel F.
  • 176
  • 1
  • 16

2 Answers2

4

Create Response class:

public class Response<T> {
    String result;
    String message;
    T customField;

    public Response(String result, String message, T customField) {
        this.result = result;
        this.message = message;
        this.customField = customField;
    }
    //getter setter
}

and then in controller:

@POST
@Path("/get_user")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response<User> getUser (String id){
    //Logic
    return new Response<User> ("result", "message", user);
}
glw
  • 1,646
  • 1
  • 16
  • 20
  • I didn't have thought this solution! Sometimes the most complex problems have the easiest solutions. – Angel F. Aug 24 '15 at 12:06
  • I am doing the same way but gettin MessageBodyProviderNotFoundException error in response – Navneet Sharma Mar 01 '18 at 17:06
  • @glw My Question is similar to this , you can check it on [link](https://stackoverflow.com/questions/48485363/how-to-create-generic-json-response-object-in-java) – Navneet Sharma Mar 01 '18 at 17:13
1

in client side you can use following code:

 public class WSClient {
   private URI uri;
   private Client client;

   public WSClient(){
   uri = UriBuilder.fromUri("pathToYoutWebservice").port(8080).build();
   client = ClientBuilder.newClient();
}

 public User getUser(String userId){

 User user = client.target(uri).path(java.text.MessageFormat.format("{0}", new Object[]{userId})).request(MediaType.APPLICATION_JSON).get(User.class);
 return user;
}
}
Vaseph
  • 704
  • 1
  • 8
  • 20