Spring download how to configure HttpMessageConverter

Back-end, Spring Boot Project ( v1.3.0.RELEASE ), put Rest JSON Api on fron-end just running into error:

Infinite recursion (StackOverflowError)

      

I decided to go to custom FastJsonHttpMessageConverter and the code below

@Bean
public HttpMessageConverter httpMessageConverter() {
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();

    return fastJsonHttpMessageConverter;
}

      

but it doesn't work, it actually uses the default HttpMessageConverter

. Despite the absence of the error above, the result is not what I expected. eg.

suppliers: [
    {
        $ref: "$.value"
    }
]

      

Now change the code above

@Bean
public HttpMessageConverter mappingJackson2HttpMessageConverter() {
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
    return fastJsonHttpMessageConverter;
}

      

This time it works, I want to know why the method name should be mappingJackson2HttpMessageConverter

? If a different method name is used, how do I set it up?

+1


source to share


2 answers


After looking at this official document , I know how to set up converters.

@Bean
public HttpMessageConverters customConverters() {
    HttpMessageConverter<?> additional = new FastJsonHttpMessageConverter();
    return new HttpMessageConverters(additional);
}

      

A Go to my main post, actually below code doesn't work.

@Bean
public HttpMessageConverter mappingJackson2HttpMessageConverter() {
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
    return fastJsonHttpMessageConverter;
}

      



Spring boot never introduces this method if you set a breakpoint in it.

And below code also works.

@SpringBootApplication
public class FooApplication extends WebMvcConfigurerAdapter {


   public static void main(String[] args) {
       SpringApplication.run(FooApplication.class, args);
  }

  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    super.configureMessageConverters(converters);
    converters.add(new FastJsonHttpMessageConverter());
  }

}

      

+2


source


Spring boot says ( https://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-customize-the-responsebody-rendering ):

If the bean being added is a type that would have been enabled by default (for example, MappingJackson2HttpMessageConverter for JSON conversions), then it will override the default.

The bean being added is not of the same type, so the above does not happen. Your converter is somewhere in the list of converters (probably at the end) and the first matching converter (old) does the job.



The beans created by the Java config has a method name, so when you create a second bean named mappingJackson2HttpMessageConverter, it overrides the spring generated JacksonHttpMessageConvertersConfiguration load and takes its place.

Instead of adding a bean converter, you might prefer to override the entire list of converters:

As with normal MVC usage, any WebMvcConfigurerAdapter beans you provide can also inject converters by overriding the configureMessageConverters method.

+1


source







All Articles