相关疑难解决方法(0)

Spring-MVC 406不可接受而不是JSON响应

我正在尝试使用Spring 3.0.6返回JSON响应,但我得到406响应"Not Acceptable",其描述为:"此请求标识的资源只能生成具有不可接受的特征的响应"请求"接受"标题()."

我知道之前已经提出了一个非常类似的问题,但是我无法让它对我的项目起作用,尽管进行了很多测试并且我不明白我做错了什么.

在我的Maven pom.xml中,我有以下内容:

<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-mapper-asl</artifactId>
  <version>1.8.5</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-core-asl</artifactId>
  <version>1.8.5</version>
  <scope>compile</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

在web.xml中,我引用webmvc-config.xml,日志确认已加载.

<servlet>
    <servlet-name>mainServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/webmvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
Run Code Online (Sandbox Code Playgroud)

在webmvc-config.xml中我有以下内容:

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
    </bean> 
    <mvc:annotation-driven />
Run Code Online (Sandbox Code Playgroud)

我的控制器是:

@Controller
public class ClassifiedController {

    @RequestMapping(value = "/classified/{idClassified}", headers = "Accept=*/*",
                    method = RequestMethod.GET)
    @ResponseBody
    public final Classified getClassified(@PathVariable final int idClassified) {
        ...
Run Code Online (Sandbox Code Playgroud)

我尝试使用或不使用headers参数,但结果相同.如果我直接使用Firefox调用URL,请求标题包含以下内容(使用firebug进行检查):

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Run Code Online (Sandbox Code Playgroud)

如果我使用以下JQuery:

$.ajax({
        url: '/classified/38001',
        type: 'GET',
        dataType: 'json' …
Run Code Online (Sandbox Code Playgroud)

java spring json spring-mvc jackson

13
推荐指数
3
解决办法
2万
查看次数

标签 统计

jackson ×1

java ×1

json ×1

spring ×1

spring-mvc ×1