Spring MVC Rest service returns json to report 406 error solution

@ResponseBody & @RequestBody

@RequestBody inserts the HTTP request body into the method, using the appropriate HttpMessageConverter to write the request body to some object.

@ResponseBody returns the content or object as the HTTP response body, using @ResponseBody will skip the view processing part and instead call the appropriate HttpMessageConverter to write the return value to the output stream.

@ResponseBody can mark any object, and Srping completes the object-protocol conversion

We see that just a few lines of configuration. After using the @ResponseBody annotation, the object returned by the Controller is automatically converted into the corresponding json data, and I have to sigh the power of SpringMVC here.

Yesterday, when @ResponseBody returned JSON format, I always reported http 406 error. I checked the configuration file carefully, and it turned out that there were two configurations. Caused by the failure of the latter one, here is a simple investigation and several solutions

The error roughly means:

HTTP Status 406 (Not Accepted) 

->Unable to respond to the requested web page with the requested content attribute.

Many of the online materials say that supportedMediaTypes needs to add application/json;charset=UTF-8, but 406 (Not Acceptable) still appears

One: Make sure applicationContext-configuration.xml is configured with <mvc:annotation-driven>

<mvc:annotation-driven>

        <mvc:message-converters>

            <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

                <property name="supportedMediaTypes">

                    <list>

                        <value>text/plain;charset=utf-8</value>

                        <value>text/html;charset=UTF-8</value>

                        <value>text/json;charset=UTF-8</value>

                        <value>application/json;charset=utf-8</value>

                    </list>

                </property>

                <property name="objectMapper">

                    <bean class="com.fasterxml.jackson.databind.ObjectMapper">

                        <property name="dateFormat">

                            <bean class="java.text.SimpleDateFormat">

                                <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/>

                            </bean>

                        </property>

                    </bean>

                </property>

            </bean>

        </mvc:message-converters>

    </mvc:annotation-driven>

二:原来springMvc使用@ResponseBody,如果返回的是json结果,需要添加jackson的jar包的依赖

<dependency>

    <groupId>org.codehaus.jackson</groupId>

    <artifactId>jackson-core-asl</artifactId>

    <version>1.9.13</version>

</dependency>

    <dependency>

        <groupId>org.codehaus.jackson</groupId>

        <artifactId>jackson-mapper-asl</artifactId>

        <version>1.9.13</version>

    </dependency>

</dependencies>

<dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-databind</artifactId>

    <version>2.8.0</version>

</dependency>

三:测试supportedMediaTypes,就算不配置application/json;charset=UTF-8,也可以正常返回结果。

<property name="supportedMediaTypes">

    <list>

        <value>text/plain;charset=utf-8</value>

        <value>text/html;charset=UTF-8</value>

        <value>text/json;charset=UTF-8</value>

        <value>application/json;charset=utf-8</value>

    </list>

 

</property>

注意:在使用@ResponseBody 返回json的时候,方法参数中一定不能他添加   PrintWriter printWriter,这就画蛇添足了,而且程序会报错

 java.lang.IllegalStateException: getWriter() has already been called for this response

 

 

 参考:

https://my.oschina.net/lichhao/blog/172562

https://my.oschina.net/HeliosFly/blog/205343

http://www.cnblogs.com/fangjian0423/p/springMVC-xml-json-convert.html

Guess you like

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