本文介绍了Java Spring JSON响应问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一些基于Spring的REST服务示例.我想获取基于Foo对象的JSON,但是当我尝试使用curl发送请求时,它显示406错误:此请求所标识的资源只能根据请求"accept"标头生成特性不可接受的响应".这是我的代码:RestControl.java

I want to create some Spring based REST service sample. I want to get JSON based on Foo object, but when I try to send request using curl, it shows 406 error: "The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.". Here is my code:RestControl.java

package hello;

import org.springframework.stereotype.*;

import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/app")
public class RestControl {
    @RequestMapping(value="/get",produces="application/json", method=RequestMethod.GET)
    @ResponseBody
    public Foo getFoo(){
        Foo f = new Foo();
        f.setId(new Long(1));
        f.setName("lol");
        return f;
    }
}

Foo.java:

package hello;

public class Foo {
    private Long id;
    private String name;

    public void setId(Long id){
        this.id=id;
    }

    public void setName(String name){
        this.name=name;
    }

    public Long getId(){
        return this.id;
    }

    public String getName(){
        return this.name;
    }

}

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>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

mvc-dispatcher-servlet.xml:

mvc-dispatcher-servlet.xml:

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


    <mvc:annotation-driven />

    <bean id="RestControl"
          class="hello.RestControl" />

    <bean id="foo"
          class="hello.Foo" />

</beans>

更新:

Upd2: org.codehaus.jackson 杰克逊·马珀·阿塞尔 1.9.13

Upd2: org.codehaus.jackson jackson-mapper-asl 1.9.13

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.2.12</version>
    </dependency>

    <!-- Spring ORM support -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

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


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

推荐答案

Spring本身不知道如何正确序列化和反序列化您的域对象,因此您需要在项目中包含Jackson来处理该问题:

Spring by itself does not know how to correctly serialize and deserialize your domain objects, you need to include Jackson in your project to handle that:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.2.2</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.2.2</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.2.2</version>
    </dependency>

除非要在流程中添加一些自定义,否则不需要任何特殊配置即可告诉Jackson处理JSON转换.

There is no special configuration required to tell Jackson to handle your JSON translation, unless you want to add some customization in the process.

这篇关于Java Spring JSON响应问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:25