2

I'm using jax rs and I want to create responses as json, code:

@Produces(MediaType.APPLICATION_JSON)
public class Rest{
@GET
Path("/Test")
public RestResponse restTest(){
return new RestResponse(100,"ERROR");
}
}

@XmlRootElement()
public class RestResponse{
private Integer code;
private String status;
private Integer id;

public RestResponse(Integer code, String status){
this.code = code;
this.status = status;
}
}

and I'm getting response as :
{"status":"ERROR","code":100,"id":null}

how to remove that "id":null value? I want response to looks as : {"status":"ERROR","code":100}

I need it because sometimes id is null and sometimes code is null and I don't want to display it.

1 Answer 1

2

With Genson you could do it like that:

// register a provider for your custom genson configuration.
@Provider
public class GensonCustomResolver implements ContextResolver<Genson> {
  // configure the Genson instance
  private final Genson genson = new Genson.Builder()
                                        // if you want to support JAXB annotations
                                        .with(new JAXBBundle())
                                        .setSkipNull(true)
                                        .create();

  @Override
  public Genson getContext(Class<?> type) {
    return genson;
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

omg that is so simply and powerfull, it makes me life easier thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.