DntFrgtDSemiCln
DntFrgtDSemiCln

Reputation: 1289

Processing JSON response using JAX-RS

I have a json payload like this:

{
   "account":    {

      "sample_id": 1424876658095,
      "parameters":       [
                  {
            "name": "email",
            "value": "[email protected]"
         },
                  {
            "name": "first_name",
            "value": "FIRSTNAME"
         },
                  {
            "name": "last_name",
            "value": "LASTNAME"
         }
      ]
   },
   "assests": [   {

      "tran_id": "1234567",

   }]
}

The above json payload is getting generated in the response of a rest API call. I would like to process this response in java to generate something like this:

{
   "account":    {

      "sample_id": 1424876658095,
      "email_id": "[email protected]",
      "first_name": "FIRSTNAME",
      "last_name": "LASTNAME",
   },
   "assets": [   {

      "tran_id": "1234567",

   }]
}

I am using the JAX-RS specification for the REST API, but I am not able to find any library to process the response.

Upvotes: 1

Views: 680

Answers (2)

Thierry Templier
Thierry Templier

Reputation: 202176

If you want to leverage the Jackson serialization within JAX-Rs, you need to implement as custom serializer.

There are two steps to do that:

  • Create the custom serializer

    Here is a sample of a custom Jackson serializer for your needs based on beans AccountBean and ParameterBean:

    public class AccountBeanSerializer extends JsonSerializer<AccountBean> {
        @Override
        public void serialize(AccountBean accountBean, JsonGenerator jgen,
                SerializerProvider provider) throws IOException,
                JsonProcessingException {
            jgen.writeStartObject();
            jgen.writeNumberField("sample_id", accountBean.getSampleId());
            List<ParameterBean> parameters = accountBean.getParameters();
            for (ParameterBean parameterBean : parameters) {
                jgen.writeStringField(parameterBean.getName(),
                        parameterBean.getValue());
            }
            jgen.writeEndObject();
        }
    }
    

    I assume the class for your response if something like that:

    class ResponseBean
        field account = class AccountBean
           field sampleId (long)
           field parameters = class ParameterBean
        (...)
    
  • Register the custom serializer

    You need then to provide a custom Jackson configuration within a context resolver. For this, you can create an implementation of the interface ContextResolver annotated with Provider.

    @Provider
    public class CustomJacksonConfig implements ContextResolver<ObjectMapper> {
        private ObjectMapper objectMapper;
    
        public JacksonConfig() {
            this.objectMapper = new ObjectMapper();
            SimpleModule module = new SimpleModule("MyModule", new Version(1, 0, 0, null));
            module.addSerializer(AccountBean.class, new AccountSerializer());
            this.objectMapper.registerModule(module);
        }
    
        public ObjectMapper getContext(Class<?> objectType) {
            return objectMapper;
        }
    }
    

Following links could help you:

Hope this helps, Thierry

Upvotes: 2

John Ament
John Ament

Reputation: 11723

You would have to implement a custom MessageBodyWriter for this to generate a different JSON payload for your POJOs than the default.

Upvotes: 0

Related Questions