弹簧错误:仅能够根据请求“接受"产生具有特征不可接受的响应.标头 [英] Spring Error: only capable of generating responses with characteristics not acceptable according to the request "accept" headers

查看:96
本文介绍了弹簧错误:仅能够根据请求“接受"产生具有特征不可接受的响应.标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring来构建Restful API,当我访问以下方法时:

Im using spring to build a Restful API and when I access the method below:

// get the entity in DB by using id number
    @RequestMapping(value = "/{id:.+}", method = RequestMethod.GET)
    public @ResponseBody
    User getEmployee(@PathVariable("id") String email) {
        User user=null;
        System.out.println(email);
        try {
            user = dataServices.getEntityById(email);

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(user.getNickname());
        return user;
    }

使用以下URL: http://localhost:8080/RestCrud/user/richard_johnson @ sina.com

它收到406错误:

我确保已添加

<mvc:annotation-driven />

在我的spring-config.xml中.

in my spring-config.xml.

我还确定我在pom.xml中添加了这些杰克逊依赖项

Im also sure I added those jackson dependencies in pom.xml

********************编辑************************** ***********

********************edit*************************************

********************再次编辑***************************** ******

********************edit again*******************************

如您所见,我没有在@RequestMapping批注中限制标头,因此我认为这与标头限制无关.

as you can see I did not restrict headers in the @RequestMapping annotation, so I dont think its an issue related to the header restriction.

此外,我的网址格式如下:

Also, my url patterns are like:

http://localhost:8080/RestCrud/user/id

我已经测试了列表" http://localhost:8080/RestCrud/user/list

Ive tested the "list" http://localhost:8080/RestCrud/user/list

它有效,但"id"路径无效

and it works, but the "id" path does not

推荐答案

似乎您已注释掉一些杰克逊依赖项 发生此错误是因为您的雇员对象无法转换为浏览器可接受的格式.您definitley希望使用json进行响应.

It seems like you have commented out a few jackson dependencies The error occured because your employee object couldnt be converted to a format accceptable by the browser. You definitley would have wanted to respond with json.

Spring 4需要以下杰克逊库

Spring 4 required the following jackson libs

  • jackson-core
  • jackson-core-sal
  • jackson-mapper-asl
  • jackson-databind

已更新:

看看您的URL模式, *.com 扩展程序正在推动spring进行内容协商,而不是验证接受标头.

Looking at your URL pattern , the *.com extension is pushing spring to perform a content negotiation rather than verifying the accept headers.

您可以使用

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false" />
</bean>

这篇关于弹簧错误:仅能够根据请求“接受"产生具有特征不可接受的响应.标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆