2

I have looked at this question and made it work using

.configure(DeserializationConfig.Feature.USE_ANNOTATIONS, true)
.configure(SerializationConfig.Feature.USE_ANNOTATIONS, true);

on my custom ObjectMapper

Json serialization works fine with the above configuration.

But the problem is I want to use Jackson 2.x now because of better deserialization control and I cannot find any good documentation indicating how to configure jackson 2.x to ignore JAXB annotations

Any help would be appreciated.

UPDATE: I tried objectMapper.configure(MapperFeature.USE_ANNOTATIONS, false); but this seems to have no effect on jackson picking up JAXB annotations.

Second Update: Posted the solution below as an answer.

Community
  • 1
  • 1
Harshdeep
  • 5,614
  • 10
  • 37
  • 45

1 Answers1

1

The solution which I found was creating my ObjectMapper and then registering that with jersey within main method (I am using SpringBoot with jersey as the complete solution).

If I created ObjectMapper in other class and tried to register it in main, neither the constructor of that class was being called nor the overridden getContext() method. So I created the ObjectMapper within the main method and registered the provider with jersey.

Snippet:

ObjectMapper mapper = new ObjectMapper();
mapper.enable(MapperFeature.USE_ANNOTATIONS);

// create JsonProvider to provide custom ObjectMapper
JacksonJsonProvider provider = new JacksonJsonProvider();
provider.setMapper(mapper);

and

register(provider);

Maven dependencies, as annotations and JacksonJsonProvider are available in different artifacts:

<dependency>
       <groupId>com.fasterxml.jackson.module</groupId>
       <artifactId>jackson-module-jaxb-annotations</artifactId>
</dependency>
<dependency>
       <groupId>com.fasterxml.jackson.jaxrs</groupId>
       <artifactId>jackson-jaxrs-json-provider</artifactId>
</dependency>

Hope it helps.

Harshdeep
  • 5,614
  • 10
  • 37
  • 45
  • On further refactoring realized that problem is mainly in registering a class with '@Provider' annotations with jersey. You can still move this configuration of ObjectMapper to a method of its own or a new class but register it via method call on object and not ClassName.class. (And also you don't need to put '@Provider' annotation on new class) – Harshdeep Nov 06 '14 at 16:14