406 error when springMVC returns json data

Problem description

When springmvc is initially set up, the @responseBody annotation is used to control springMvc to return data in json format, and it will be found that the execution of the server is not abnormal, but a 406 error is displayed on the browser side. This error is because the server side returns data to the browser. Not parsed correctly by the browser.

The solution
is generally to control the header property of the response object. SpringMVC can use the annotation-driven tag to solve this problem, which is to configure <mvc:annotation-driven>, remember to introduce the mvcxsd namespace before configuring this, otherwise spring cannot correctly parse this tag, the steps are as follows.

1. First introduce
xmlns:mvc="http://www.springframework.org/schema/mvc"


2. Then add
http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd


3. Finally configure this annotation, the fastJson parser I use here (remember to introduce this jar package).
<mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <!-- Avoid when IE executes AJAX,
            <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>After


configuring the above content, the 406 error will not appear, I hope it can help everyone!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326691177&siteId=291194637