Spring mvc : Changing default Response format from xml to json

后端 未结 3 756
粉色の甜心
粉色の甜心 2021-01-17 10:07

I have gone through other similar asked questions but nothing worked for me.

All my API\'s return JSON as response by Default:

3条回答
  •  爱一瞬间的悲伤
    2021-01-17 11:05

    For me, adding

    @Configuration
    public class WebConfig extends WebMvcConfigurerAdapter {
    
        @Override
        public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
            configurer.defaultContentType(MediaType.APPLICATION_JSON_UTF8);
        }
    
    }
    

    solved the problem.

    Now by default all RestControllers return JSON, if no Accept header in the request. Also if Accept: application/xml header is passed, then result is XML.

    Also, worth reading: https://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc

提交回复
热议问题