简体   繁体   中英

Processing JSON response using JAX-RS

I have a json payload like this:

{
   "account":    {

      "sample_id": 1424876658095,
      "parameters":       [
                  {
            "name": "email",
            "value": "hello@xyz.com"
         },
                  {
            "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": "hello@xyz.com",
      "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.

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

为此,您将必须实现一个自定义MessageBodyWriter才能为您的POJO生成不同于默认值的JSON负载。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM