尝试在@ResponseBody中返回对象时出现Spring 406错误

时间:2014-04-30 09:43:05

标签: spring serialization

我的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>

其余服务-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"/>

其余-调度-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;
    }
}

}

...并且请求给我一个406error:

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

有关解决这个问题的任何建议吗?请帮忙。

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

1 个答案:

答案 0 :(得分:0)

您不打算在此处添加@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