zhuguowei
zhuguowei

Reputation: 8487

How to know which message converter is used by spring boot?

For some reason I decided to change to another message converter, my code is below

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

Now I'd like to know how to check whether this custom converter is in effect? I tried to access /beans but only got this

{
    bean: "customConverters",
    scope: "singleton",
    type: "org.springframework.boot.autoconfigure.web.HttpMessageConverters",
    resource: "com.foo.BarApplication",
    dependencies: [ ]
}

So does exist some manner to know which message converter is used by spring boot?

Upvotes: 2

Views: 3647

Answers (2)

zhuguowei
zhuguowei

Reputation: 8487

Because I'm not very sure if my custom converter works, so I have to track source code. I cloned spring framework then attached it in eclipse, then debug step by step and found something. In AbstractMessageConverterMethodProcessor.writeWithMessageConverters

for (HttpMessageConverter<?> messageConverter : this.messageConverters)

when watch this.messageConverters got below output

[org.springframework.hateoas.mvc.TypeConstrainedMappingJackson2HttpMessageConverter@1eb0d2e8, com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter@2026476b, org.springframework.http.converter.ByteArrayHttpMessageConverter@3287cbc7, org.springframework.http.converter.StringHttpMessageConverter@2c19dd3, org.springframework.http.converter.ResourceHttpMessageConverter@1afe28f1, org.springframework.http.converter.xml.SourceHttpMessageConverter@87129da, org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@4a412e0, org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@a8528a2, org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@61d720a3]

and actually it used com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter@2026476b

Upvotes: 2

mzc
mzc

Reputation: 3355

Only injected dependencies are shown in the dependencies array. If you do something like

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

@Bean
@Autowired
public HttpMessageConverters convertersToBeUsed(FastJsonHttpMessageConverter converter) {
    return new HttpMessageConverters(converter);
}

you will see the FastJsonHttpMessageConverter in the list.

If you want to see all registered converters, look for HttpMessageConvertersAutoConfiguration in the bean list. It should look similar to this:

{
   bean: "org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration",
   scope: "singleton",
   type: "org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration$$EnhancerBySpringCGLIB$$9e15b021",
   resource: "null",
   dependencies: [
       "fastJsonHttpMessageConverter",
       "stringHttpMessageConverter"
   ]
}

Upvotes: 1

Related Questions