【问题标题】:Spring 406 error while trying to return object in @ResponseBody尝试在@ResponseBody 中返回对象时出现 Spring 406 错误
【发布时间】:2014-06-16 13:47:34
【问题描述】:

我的 web.xml:

<servlet>
    <servlet-name>rest-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>rest-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

pom.xml:

...
<jackson.mapper.version>1.9.13</jackson.mapper.version>
...
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-lgpl</artifactId>
        <version>${jackson.mapper.version}</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>${jackson.mapper.version}</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-lgpl</artifactId>
        <version>${jackson.mapper.version}</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${jackson.mapper.version}</version>
    </dependency>

rest-services-config.xml:

<mvc:annotation-driven />
<context:component-scan base-package="com.example.web.rest" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
    <property name="contentType" value="text/plain"/>
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <util:list id="beanList">
            <ref bean="jsonMessageConverter"/>
        </util:list>
    </property>
</bean>

<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>

rest-dispatcher-servlet.xml:

<context:annotation-config />
<context:component-scan base-package="com.example.web.rest" />



<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.html</value>
    </property>
</bean>

<mvc:resources mapping="/WEB-INF/pages/**" location="/WEB-INF/pages/" />
<mvc:resources mapping="/vendors/**" location="/resources/vendors/" />
<mvc:resources mapping="/controls/**" location="/resources/controls/" />
<mvc:resources mapping="/css/**" location="/resources/css" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />

最后,这是我的控制器:

@Controller
@RequestMapping("/user")
public class UserService {

@Autowired(required=true)
private UserDaoImpl dao;

@RequestMapping(value="/getCurrentUserCredentials/", method = RequestMethod.GET)
public @ResponseBody UserCredentials getCurrentUserCredentials() {
    HttpSession session = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getSession();
    try {
        return dao.getCurrentUserCredentials((Integer)session.getAttribute("userid"));
    } catch (Exception e) {
        return null;
    }
}

}

...和请求给我一个 406 错误:

    $.ajax({
            type: 'GET',
            url: 'user/getCurrentUserCredentials/',
            dataType: 'json',
            success: function(data) {console.log(data); self.currentUserCredentials(data);},
            error: function() {console.log('error');}
        });

关于解决这个问题有什么建议吗?请帮忙。

控制器的方法返回正确的数据(在调试中检查),而这些数据无法正确处理。

【问题讨论】:

  • 尝试将rest-services-config.xml中的MappingJacksonView改成&lt;bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"&gt; &lt;property name="contentType" value="application/json"/&gt; &lt;/bean&gt;
  • 不幸的是,“406”还在
  • getCurrentUserCredentials 是否抛出异常?
  • Nono 正如我所说 - 这种方法效果很好;Obv.我在设置中的某个地方有错误
  • 您是否尝试过使用另一个 REST 客户端而不是 jquery?原因是有一个类似的问题指出 jquery 不正确,stackoverflow.com/questions/4069903/…

标签: spring serialization


【解决方案1】:

您不是要在此处添加@Produces 注释吗?

@RequestMapping(value="/getCurrentUserCredentials/", method = RequestMethod.GET, produces="application/json")
public @ResponseBody UserCredentials getCurrentUserCredentials() {
    HttpSession session = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getSession();
    try {
        return dao.getCurrentUserCredentials((Integer)session.getAttribute("userid"));
    } catch (Exception e) {
        return null;
    }
}

不确定这是否重要,但 @ResponseBody 注释也在 public 关键字之前。我的例子就是基于这个Spring reference code

@Controller
public class ErrorController {

    @RequestMapping(value="/error", produces="application/json")
    @ResponseBody
    public Map<String, Object> handle(HttpServletRequest request) {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", request.getAttribute("javax.servlet.error.status_code"));
        map.put("reason", request.getAttribute("javax.servlet.error.message"));

        return map;
    }

}

对于较新版本的 Spring,还可以选择使用 @RestController 而不是 @Controller

【讨论】:

  • 嗨,尝试在“public ...”之前添加“@ResponseBody”,但似乎没关系。添加了“produce”参数,但仍然是 406。
  • @Arti88,您是否也尝试过请求映射的生产部分(即produces="application/json")?
  • 是的...还是一样:加载资源失败:服务器响应状态为 406(不可接受)localhost:8080/PR2/user/getCurrentUserCredentials
猜你喜欢
  • 2015-04-26
  • 2018-01-10
  • 2016-07-08
  • 2014-04-11
  • 2014-12-12
  • 1970-01-01
  • 1970-01-01
  • 2013-02-19
  • 2011-11-12
相关资源
最近更新 更多