I am having a problem with a newly created Spring Boot application. For some reason something is adding two MappingJackson2HttpMessageConverter
instances to the return value processing of Spring Web MVC (I found this out using the debugger).
One of them uses the ObjectMapper
instance I create via a @Bean
method in my configuration class, the other one uses some random other instance. This 2nd ObjectMapper
is therefor completely unconfigured and my ObjectMapper
configurations are not applied to the JSON serialization used by @ResponseBody
.
How can I stop this duplication? You can find all of my code below.
Edit: This problem is not related to my custom ObjectMapper
bean. Even if I remove it (completely empty configuration except @SpringBootApplication
), I still get two MappingJackson2HttpMessageConverter
instances, one of which uses a completely unconfigured and unreachable (as far as I can tell) ObjectMapper
.
Edit²: This is not an issue of duplicate beans. ApplicationContext.getBeansOfType
returns only one instance for both ObjectMapper
and MappingJackson2HttpMessageConverter
no matter if I create the ObjectMapper
myself or let it be created by spring autoconfiguration.
build.gradle:
buildscript {
ext.springBootVersion = '2.0.0.RC1'
repositories {
mavenCentral()
maven { url 'http://repo.spring.io/snapshot' }
maven { url 'http://repo.spring.io/milestone' }
maven { url 'http://repo.spring.io/libs-snapshot' }
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion"
}
}
group 'de.takeweiland.springtest'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
repositories {
mavenCentral()
maven { url 'http://repo.spring.io/snapshot' }
maven { url 'http://repo.spring.io/milestone' }
maven { url 'http://repo.spring.io/libs-snapshot' }
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-web"
}
Configuration class:
package main;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class Main {
@Bean
public ObjectMapper createObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
return mapper;
}
public static void main(String[] args) {
SpringApplication.run(Main.class);
}
}
Controller:
package main;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@GetMapping("**")
@ResponseBody
public String test() {
return "Hello World";
}
}