Spring MVC + JSON = 406不可接受 [英] Spring MVC + JSON = 406 Not Acceptable

查看:85
本文介绍了Spring MVC + JSON = 406不可接受的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成一个简单的JSON响应。现在我得到406 Not Acceptable错误。 Tomcat说:此请求标识的资源只能根据请求接受标题生成具有不可接受特征的响应。即使我的接受标题是

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

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



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

web.xml

 < web-app id =WebApp_IDversion =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)请求,
HttpServletResponse响应)抛出异常{

ModelAndView model = new ModelAndView(HelloWorldPage);
返回模型;
}

@RequestMapping(value =foobar.htm,method = RequestMethod.GET)
public @ResponseBody Foo getShopInJSON(){
Foo f = new FOO();
f.setX(1);
f.setY(2);
f.setDescription(desc);
返回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)
公共类Foo实现Serializable
{
private int x,y;
字符串描述;
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 =jsonConverterclass =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-所有 jackson-asl jackson-core-asl 但输出相同。

解决方案


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


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


I'm trying to generate a simple JSON response working. Right now I get 406 Not Acceptable error. Tomcat says "The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers." even though my Accept headers are

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

In tomcat/lib I have all Tomcat jars, Spring jars and jackson-all-1.9.0.jar. I'm using Spring 3.2.2 with Tomcat 7.

I'm aware that this issue has been discussed many times, but none of solutions is working for me.

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;
    }
}

I've already tried adding

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

to my dispatcher-servlet.xml or changing jakcson-all to jackson-asl and jackson-core-asl but output was the same.

解决方案

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

That should be the problem. JSON is served as application/json. If you set the Accept header accordingly, you should get the proper response. (There are browser plugins that let you set headers, I like "Poster" for Firefox best)

这篇关于Spring MVC + JSON = 406不可接受的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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