org.springframework.web.HttpMediaTypeNotAcceptableException Spring MVC 4.2.5 [英] org.springframework.web.HttpMediaTypeNotAcceptableException Spring MVC 4.2.5

查看:108
本文介绍了org.springframework.web.HttpMediaTypeNotAcceptableException Spring MVC 4.2.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的Spring Controller获取json响应.我低于异常

Im trying to get json response from my Spring Controller. Im getting below exception

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

我在pom.xml中有与杰克逊相关的罐子,如下所示

I have jackson related jars in pom.xml as below

<properties>
    <spring.version>4.2.5.RELEASE</spring.version>
    <jstl.version>1.2</jstl.version>
    <servletapi.version>2.5</servletapi.version>
    <jackson.version>2.6.3</jackson.version>
</properties>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${jackson.version}</version>
    </dependency>

下面是我的控制器

    @ResponseBody
    @RequestMapping(value = "/getNodes.htm",produces="application/json")
    public List<NodeDTO> getNodes() {
...
return nodes;
}

我在浏览器中收到406错误,并且服务器日志中出现上述异常.我的浏览器正在正确发送具有application/json的Accept标头.但是我仍然面临着这个问题.我参加了很多论坛,但都没有解决我的问题.请帮助我解决问题

Im getting 406 error in the browser and above exception in server log. My browser is properly sending Accept header has application/json. But still Im facing this issue. I have gone through many forums none of them resolving my issue. Please help me to resolve it

推荐答案

您的@RequestMapping批注不正确,尤其是扩展名.htm的使用.

Your @RequestMapping annotation is incorrect, in particular the use of the extension .htm.

应该是

@RequestMapping(value = "/getNodes.json",produces="application/json")

或者也许

@RequestMapping(value = "/getNodes",produces="application/json")

Spring MVC使用URL的扩展名来标识要返回的内容的类型.在您的情况下,您要指定.htm的扩展名,Spring会将其解释为HTML.但是,这与注释的produces属性(指定JSON)冲突.

Spring MVC uses the extension of the URL to identify the type of content to return. In your case, you're specifying an extension of .htm, which Spring interprets as HTML. That however conflicts with the produces property of your annotation, which specifies JSON.

我尚不清楚Spring如何处理类似的冲突内容类型,但是如果用.json替换.htm扩展名或完全删除扩展名,则不必担心.

It's not clear to me exactly how Spring handles conflicting content types such as this, but if you replace the .htm extension with .json or drop the extension altogether, then you don't need to worry about it.

这篇关于org.springframework.web.HttpMediaTypeNotAcceptableException Spring MVC 4.2.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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