SpringMVC 无法返回 Json 或 Xml,Http 406

我已经导入并@ResponseBody添加了 jar 文件,没有任何反应我也使用过


<mvc:annotation-driven/>

没发生什么事。我整天都在受苦


调度程序-servlet.xml


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:context="http://www.springframework.org/schema/context"

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

       xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

           http://www.springframework.org/schema/context

           http://www.springframework.org/schema/context/spring-context-3.0.xsd

           http://www.springframework.org/schema/mvc

           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"

       default-autowire="byName">


    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

        <property name="messageConverters">

            <list>

                <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>

                <bean class="org.springframework.http.converter.StringHttpMessageConverter">

                    <property name="supportedMediaTypes">

                        <list>

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

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

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

                        </list>

                    </property>

                </bean>

                <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>

            </list>

        </property>

    </bean>


    <mvc:resources location="/static/" mapping="/static/**"/>

    <mvc:annotation-driven/>

    <context:component-scan base-package="org.format.demo.controller" />


    <import resource="classpath:springConfig/viewConfig/freemarker.xml"/>



</beans>


慕虎7371278
浏览 103回答 2
2回答

陪伴而非守候

尝试如下更改&nbsp; &nbsp;@RequestMapping(value="/testCustomObj",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;produces={"application/json; charset=UTF-8"},&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;method = RequestMethod.GET)&nbsp; &nbsp;@ResponseBody&nbsp; &nbsp;public Employee testCustomObj(@RequestParam(value = "id") int id,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@RequestParam(value = "name") String name)&nbsp;&nbsp;我碰巧在 Chrome 中安装了JSONView扩展,所以它可能会添加"application/json"到接受标头中,但我对此表示怀疑。您可能还想使用Postman之类的工具来完全控制要发送到 REST API 的标头。

江户川乱折腾

我找到了我得到 406 的原因,我注释了 bean RequestMappingHandlerAdapter 配置,我发现它有效,然后我在方法 afterPropertiesSet() 中调试 RequestHandlerMapping.java,我调试 this.returnValueHandlers,我发现有 7 个 messageConverters,我没有映射Jackson2HttpMessageConverter 和 Jaxb2RootElementHttpMessageConverter我缺乏<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/><bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>我在 dispatcher-servlet.xml 中添加了两个 bean,然后它就可以工作了。MappingJackson2HttpMessageConverter 可以解析Json。Jaxb2RootElementHttpMessageConverter 可以解析 Xml。最终的 dispatcher-servlet.xml 是这样的<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"&nbsp; &nbsp; &nbsp; &nbsp;xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&nbsp; &nbsp; &nbsp; &nbsp;xmlns:context="http://www.springframework.org/schema/context"&nbsp; &nbsp; &nbsp; &nbsp;xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"&nbsp; &nbsp; &nbsp; &nbsp;xsi:schemaLocation="http://www.springframework.org/schema/beans&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http://www.springframework.org/schema/beans/spring-beans-3.0.xsd&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http://www.springframework.org/schema/context&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http://www.springframework.org/schema/context/spring-context-3.0.xsd&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http://www.springframework.org/schema/mvc&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"&nbsp; &nbsp; &nbsp; &nbsp;default-autowire="byName">&nbsp; &nbsp; <!-- 防止@ResponseBody出现的中文乱码 -->&nbsp; &nbsp; <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">&nbsp; &nbsp; &nbsp; &nbsp; <property name="messageConverters">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <list>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <bean class="org.springframework.http.converter.StringHttpMessageConverter">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <property name="supportedMediaTypes">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <list>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <value>text/plain;charset=UTF-8</value>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <value>text/json;charset=UTF-8</value>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <value>text/xml;charset=UTF-8</value>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </list>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </property>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </bean>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </list>&nbsp; &nbsp; &nbsp; &nbsp; </property>&nbsp; &nbsp; </bean>&nbsp; &nbsp;&nbsp; &nbsp; <!-- 防止@ResponseBody出现的中文乱码 -->&nbsp; &nbsp; <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->&nbsp; &nbsp; <mvc:resources location="/static/" mapping="/static/**"/>&nbsp; &nbsp; <mvc:annotation-driven/>&nbsp; &nbsp; <context:component-scan base-package="org.format.demo.controller" />&nbsp; &nbsp; <!--&nbsp; &nbsp; <import resource="classpath:springConfig/viewConfig/jsp.xml"/>&nbsp; &nbsp; -->&nbsp; &nbsp; <import resource="classpath:springConfig/viewConfig/freemarker.xml"/></beans>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java