Spring-boot: Unable to customize the Jackson ObjectMapper

Created on 24 Dec 2014  路  3Comments  路  Source: spring-projects/spring-boot

Spring Boot version: 1.2.0.RELEASE
jackson-datatype-jsr310 version: 2.4.4

I wanted to customize ObjectMapper by overriding default ObjectMapper bean as stated in documentation (http://docs.spring.io/spring-boot/docs/1.2.0.RELEASE/reference/htmlsingle/#howto-customize-the-jackson-objectmapper), but it is not picked up by Spring Boot. Same goes with MappingJackson2HttpMessageConverter it is also not picked up by Spring Boot.

Bean definition looks like this:

    @Bean
    @Primary
    public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
        return builder
                .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                .modules(new ArrayList<>()) // disable module detection, in particular jsr310
                .build();
    }

If preceding bean would be used, then following test should fail (because none module will be registered in ObjectMapper instance), but it pass:

    @Test
    public void defaultObjectMapperShouldBeAbleToSerializeLocalDateTime() {
                when().
                        get("/movies/" + movies[0].getId())
                .then()
                        .statusCode(200).contentType(ContentType.JSON)
                        .body("createdDate[0]", is(2014))
                        .body("createdDate[1]", is(1))
                        .body("createdDate[2]", is(2))
                        .body("createdDate[3]", is(3))
                        .body("createdDate[4]", is(4));
    }

Here is simplified project, which reproduces the issue: (https://github.com/Dasiu/jackson-issue-reproduction-project)

Am I doing something wrong or it is a bug?

Most helpful comment

Hi,
i know this is closed, but if someone is kind of lost and comes here when using spring-cloud-contract and want's to get lowercase with underscores, or another modification on the objectmapper, i had to configure the RestAssuredMockMvc with:

MockMvc mockMvc = MockMvcBuilders
                .standaloneSetup(controller)
                .setMessageConverters(mappingJackson2HttpMessageConverter) // autowired from the context
                .build();

        RestAssuredMockMvc.mockMvc(mockMvc);

instead the config from the examples:

RestAssuredMockMvc.standalone(controller);

hope that helps

p.s. for reference, i got inspired by https://stackoverflow.com/questions/31883657/customized-objectmapper-not-used-in-test and this bug report

best
nas

All 3 comments

I think the problem is that the restassured library is completely ignoring the Spring Boot configuration. I'm not that familiar with the library but it looks like the following will use default ObjectMapper settings:

RestAssuredMockMvc.standaloneSetup(movieController);

I tried the following and got a JSON path error:

@Autowired
private WebApplicationContext context;

// ...
    RestAssuredMockMvc.webAppContextSetup(context);

I also tried hitting the service directly from a browser and got this:

{

    "title": "Batman",
    "id": 1,
    "createdDate": {
        "year": 2014,
        "month": "JANUARY",
        "dayOfMonth": 2,
        "dayOfWeek": "THURSDAY",
        "dayOfYear": 2,
        "monthValue": 1,
        "hour": 3,
        "minute": 4,
        "second": 0,
        "nano": 0,
        "chronology": {
            "id": "ISO",
            "calendarType": "iso8601"
        }
    },
    "lastModifiedDate": null
}

Closing for now. If I'm wrong about the problem being resteasy perhaps you could create a test without the library. The Tomat sample might be a good starting point.

Hi,
i know this is closed, but if someone is kind of lost and comes here when using spring-cloud-contract and want's to get lowercase with underscores, or another modification on the objectmapper, i had to configure the RestAssuredMockMvc with:

MockMvc mockMvc = MockMvcBuilders
                .standaloneSetup(controller)
                .setMessageConverters(mappingJackson2HttpMessageConverter) // autowired from the context
                .build();

        RestAssuredMockMvc.mockMvc(mockMvc);

instead the config from the examples:

RestAssuredMockMvc.standalone(controller);

hope that helps

p.s. for reference, i got inspired by https://stackoverflow.com/questions/31883657/customized-objectmapper-not-used-in-test and this bug report

best
nas

Was this page helpful?
0 / 5 - 0 ratings