Include Null or Empty Attributes in Response of Jax-Rs Rest Service

 By Default when the response has empty or null attributes it is removed from the response structure to reduce payload size and to improve performance. But there are times when a complete structure is needed despite its attributes having a null or empty value.

Below process shows how to modify a Jax-Rs application to include null or empty attributes in response structure.

  • Create a sample Jax-Rs Application
                Create a basic Jax-Rs Application

  • Add another param (param3) to both request and response structure and create a pojo class for the same
    • Request:
    • Response :
  • Now in Sample Rest service add logic to map the Request param3 attribute to Response param3 attribute, this way we can test the Service by passing null as input parameters to check the response

  • In most of the cases this is what may be expected but in some cases we may want to have all attributes as part of response despite having a null value.
  • For the next part we need a third party GSON library, we can download the same from Git.
  • Once the library is downloaded place the same in WebService Project folder and add it to XxcomSampleJaxRsWebService Project.




  • Now we need to modify all Response classes to show empty or null attributes , for this we will create a dummy interface in XxcomSampleJaxRsService Project and make sure all Response classes implement the same, this way we will be able to apply our configuration changes to Response classes alone.

  • Create a new Class JsonBodyWriter inside XxcomSampleJaxRsWebService Project which implements MessageBodyWriter Class.

  • Edit this class to include our previously created Json interface as shown below.


  • We need to recreate overridden methods.







  • Edit the JsonBodyWriterClass as below.
@Produces(MediaType.APPLICATION_JSON)
@Provider
public class JsonBodyWriter implements MessageBodyWriter<Json>{
    public JsonBodyWriter() {
        super();
    }

    @Override
    public boolean isWriteable(Class<?> c, Type type, Annotation[] annotation, MediaType mediaType) {
        System.out.println("Inside isWriteable..");
        return true;
    }

    @Override
    public long getSize(Json json, Class<?> c, Type type, Annotation[] annotation, MediaType mediaType) {
        // TODO Implement this method
        return 0L;
    }

    @Override
    public void writeTo(Json json, Class<?> c, Type type, Annotation[] annotation, MediaType mediaType,
                        MultivaluedMap<String, Object> multivaluedMap, OutputStream outputStream) throws IOException,
                                                                                                         WebApplicationException {
        System.out.println("Inside writeTo..");
        Gson gson = new GsonBuilder().serializeNulls().create();
        outputStream.write(gson.toJson(json).getBytes());

    }
}

  • Run the Service and test the same via Postman

Comments

Popular posts from this blog

Mavenize an Oracle ADF Application

Creating Column Filter on a VBCS Table using ListDataProvider(LDP)

Using Custom Colors in Sunburst Chart