当前位置: 首页 > 面试题库 >

Spring MVC + JSON = 406 Not Acceptable

贺景铄
2023-03-14
问题内容

我正在尝试生成一个简单的JSON响应。现在,我收到406不可接受的错误。Tomcat说:“此请求标识的资源只能根据请求“接受”标头生成特性不可接受的响应。” 即使我的Accept标题是

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

在tomcat / lib中,我有所有的Tomcat jar,Spring jar和jackson-all-1.9.0.jar。我在Tomcat 7中使用Spring 3.2.2。

我知道这个问题已经讨论了很多次,但是没有一种解决方案对我有用。

web.xml

<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <display-name>Spring Web MVC Application</display-name>

  <servlet>
    <servlet-name>dispatcher</servlet-name>
        <servlet-class>
                  org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
  </servlet-mapping>

</web-app>

dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
 <context:component-scan base-package="com.smiechmateusz.controller" />
 <context:annotation-config />

    <mvc:annotation-driven />

</beans>

HelloWorldController.java

package com.smiechmateusz.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.smiechmateusz.dao.Foo;

@Controller
@RequestMapping("/")
public class HelloWorldController extends AbstractController{

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        ModelAndView model = new ModelAndView("HelloWorldPage");
        return model;
    }

    @RequestMapping(value="foobar.htm", method = RequestMethod.GET)
    public @ResponseBody Foo getShopInJSON() {
        Foo f = new Foo();
        f.setX(1);
        f.setY(2);
        f.setDescription("desc");
        return f;
    }
}

Foo.java

package com.smiechmateusz.dao;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="foobaz")
public class Foo implements Serializable
{
    private int x, y;
    String description;
    int id;

    @Column(name = "x")
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    @Column(name = "y")
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    @Column(name = "description")
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }

    @Id @GeneratedValue
    @Column(name = "id")
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
}

我已经尝试添加

<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
          <list>
            <ref bean="jsonConverter"/>
          </list>
    </property>
</bean>

到我的dispatcher-servlet.xml或将jakcson-all更改为jackson-asl和jackson-core-asl,但输出是相同的。


问题答案:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8

那应该是问题。JSON用作application/json。如果相应地设置了Accept标头,则应该得到正确的响应。(有一些浏览器插件可让你设置标题,我最喜欢Firefox的“海报”)



 类似资料:
  • 我有过 我通过这种方式传递profileJson: 但是我的配置文件Json对象具有所有空字段。我应该怎么做才能让Spring解析我的json?

  • 我在向spring mvc控制器发送请求时遇到了一些问题。我有实体: 我收到一个HTTP/1.1400错误请求: 我做错了什么?

  • 我正在从事一个SpringMVC项目,我需要完成的任务之一是让用户在POST请求中发送一个JSON数据字符串。我知道Spring将使用Jackson对对象反序列化JSON,但如果我尝试以下方法: 我只是返回HTTP 400错误请求(“客户端发送的请求在语法上不正确”)。 如何获取客户端作为字符串发送的原始JSON?

  • 本文向大家介绍Springmvc ResponseBody响应json数据实现过程,包括了Springmvc ResponseBody响应json数据实现过程的使用技巧和注意事项,需要的朋友参考一下 该注解用于将 Controller 的方法返回的对象,通过 HttpMessageConverter 接口转换为指定格式的数据如:json,xml 等,通过 Response 响应给客户端 示例 需求

  • 本文向大家介绍Springmvc如何返回xml及json格式数据,包括了Springmvc如何返回xml及json格式数据的使用技巧和注意事项,需要的朋友参考一下 问:@ResponseBody注解怎么指定返回xml 还是json 答:@RequestMapping 的produces 属性指定 produces = "application/xml" 或者 produces = "applica

  • 本文向大家介绍详解SpringMVC @RequestBody接收Json对象字符串,包括了详解SpringMVC @RequestBody接收Json对象字符串的使用技巧和注意事项,需要的朋友参考一下 页面提交请求参数有两种,一种是form格式提交,一种json格式提交 通常情况下我们使用的都是form格式提交的数据,数据格式:k=v&k=v,这个时候用springMVC接收参数没有问题,但有时

  • 本文向大家介绍Springmvc发送json数据转Java对象接收,包括了Springmvc发送json数据转Java对象接收的使用技巧和注意事项,需要的朋友参考一下 1、导包 基于maven 2、jsp代码 3、控制器代码 4、配置json转换器 如果不使用注解驱动<mvc:annotation-driven />,就需要给处理器适配器配置json转换器 在springmvc.xml配置文件中,

  • 本文向大家介绍springmvc json类型转换错误解决方案,包括了springmvc json类型转换错误解决方案的使用技巧和注意事项,需要的朋友参考一下 这篇文章主要介绍了springmvc json类型转换错误解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 在用springmvc做json数据时,返回时,提示类型转换错误 如下,