Spring 406尝试返回@ResponseBy中的对象时出错

Spring 406尝试返回@ResponseBy中的对象时出错,spring,serialization,Spring,Serialization,my 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> </servle

my 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>
}

…以及给我一个406错误的请求:

    $.ajax({
            type: 'GET',
            url: 'user/getCurrentUserCredentials/',
            dataType: 'json',
            success: function(data) {console.log(data); self.currentUserCredentials(data);},
            error: function() {console.log('error');}
        });
关于解决这个问题有什么建议吗?请帮忙


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

您不打算在此处添加@products注释吗

@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,还可以选择使用@RestController而不是@Controller。

尝试将rest-services-config.xml中的MappingJacksonView更改为406 stillIs getCurrentUserCredentials是否引发异常?如我所说,否-此方法非常有效;我在设置中的某个地方出错了您是否尝试使用另一个rest客户端而不是jquery?原因是,有一个类似的问题指出jquery是不正确的,嗨,在公开之前试图预先添加@ResponseBody。。。但似乎这并不重要。添加了product参数,但仍然是406。@Arti88,您是否也尝试了请求映射的products部分,即products=application/json?是。。。仍然相同:加载资源失败:服务器以406不可接受的状态响应
<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;
    }
}
    $.ajax({
            type: 'GET',
            url: 'user/getCurrentUserCredentials/',
            dataType: 'json',
            success: function(data) {console.log(data); self.currentUserCredentials(data);},
            error: function() {console.log('error');}
        });
@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;
    }
}
@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;
    }

}