1

After upgrading from Jersey 1.19 to Jersey 2.25, My json response is changed.

I have a property as below.

 @XmlElementWrapper(name = "items")
 @XmlElement(name = "contact")
 public List<Contact> items = new ArrayList<Contact>();

In Jersey 1.19

JSON

{
    "items": [
        {
        "id": "510651"
        }
    ]
}

Xml

<items>
    <contact>
        ..
        ..
    </contact>
</items>

After Upgrading to jersey 2.25, the xml seems to be fine, but there is issue with Json.

Json After upgrade

{
  "contact": [
        {
          "id": "510651"
        }
    ]
}

My Jersey Config is below

 public JerseyConfig() {
    packages("my.api");
    property(ServerProperties.WADL_FEATURE_DISABLE, true);
    register(RequestContextFilter.class);
    register(JacksonFeature.class);
    register(CacheControlFilter.class);
    register(GZipEncoder.class);
    register(new LoggingFeature(Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME), Level.INFO,
        LoggingFeature.Verbosity.PAYLOAD_TEXT, 10 * 1024));
    register(new AbstractBinder() {
      @Override
      public void configure() {
        bindFactory(LocaleFactory.class).to(Locale.class).in(RequestScoped.class);
      }
    });
  }
Patan
  • 17,073
  • 36
  • 124
  • 198

1 Answers1

1

Jersey uses jackson for generating JSON and in jackson 2.x this feature is changed. You can found more detail here

So, if you want to have similar behaviour, then you need to configure USE_WRAPPER_NAME_AS_PROPERTY_NAME mapperFeature -

ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME, true);

Now, this custom object mapper needs to be registered in Jersey. Check this for details

Community
  • 1
  • 1
Vikas Sachdeva
  • 5,633
  • 2
  • 17
  • 26