Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a reference to the Jackson Object Mapper in a jersey2 / hk2 application

I have a jersey2 application configured for JSON support via Jackson, adding

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>${jersey.version}</version>
</dependency>

in the POM file and

public MyApplication() {
    ...
    register(JacksonFeature.class)
    ...
}

in my application. Everything works, my resources get deserialized POJOs as arguments

@POST @Consumes(MediaType.APPLICATION_JSON)
public void blah(MyPojo p) {
    ...
}

Now one of thoese resources needs a reference to Jackson's ObjectMapper to do some deserialization on its own. I've tried doing something like

@Inject
public MyResource(@Context ObjectMapper mapper) {
    ...
}

or

@GET
public String foo(@Context ObjectMapper mapper) {
    ...
}

but in both cases the reference to mapper is null. How can I inject a reference to the ObjectMapper in my resources?

like image 312
agnul Avatar asked May 15 '15 11:05

agnul


People also ask

How does Jackson ObjectMapper work?

The Jackson ObjectMapper can parse JSON from a string, stream or file, and create a Java object or object graph representing the parsed JSON. Parsing JSON into Java objects is also referred to as to deserialize Java objects from JSON. The Jackson ObjectMapper can also create JSON from Java objects.

Does spring boot have Jackson ObjectMapper?

Overview. When using JSON format, Spring Boot will use an ObjectMapper instance to serialize responses and deserialize requests. In this tutorial, we'll take a look at the most common ways to configure the serialization and deserialization options. To learn more about Jackson, be sure to check out our Jackson tutorial.

Should I declare Jackson's ObjectMapper as a static field?

Yes, that is safe and recommended.

How does Jackson work in spring boot?

Spring Boot and Jackson The above dependency declaration will work for other Java projects, but in a Spring Boot application, you may encounter errors such as this. The Spring Boot parent POM includes Jackson dependencies. When you include the version number, it overrides the Spring Boot curated dependency versions.


1 Answers

Aside from the JacksonFeature you need to register a ContextResolver for ObjectMapper.

Simple example from the Documentation at 9.1.4.2. Configure and register

@Provider
public class MyObjectMapperProvider implements ContextResolver<ObjectMapper> {

    final ObjectMapper defaultObjectMapper;

    public MyObjectMapperProvider() {
        defaultObjectMapper = createDefaultMapper();
    }

    @Override
    public ObjectMapper getContext(Class<?> type) {
        return defaultObjectMapper;
    }

    private static ObjectMapper createDefaultMapper() {
        final ObjectMapper result = new ObjectMapper();
        result.configure(Feature.INDENT_OUTPUT, true);

        return result;
    }

    // ...
}

Complete code example available on Github

You will also need to register it

        .register(MyObjectMapperProvider.class)  
like image 185
Laurentiu L. Avatar answered Nov 15 '22 19:11

Laurentiu L.