如何在Spring 3.1中使用RESTful webservice获取JSON响应

时间:2014-09-01 05:43:43

标签: json spring rest jackson

我想生成我的对象的JSON响应我为此编写代码但没有获得成功它给出了错误406我没有使用maven我正在使用ant

这是我的控制器

@Controller

public class LoginRestCtrl {
    @RequestMapping(value = "/test", method=RequestMethod.GET)
    @ResponseBody
    public  Employee checklogin(){
        Employee e=new Employee();
        e.setEmailId("sdosi@cur.com");
        e.setId(1);
        e.setPassword("a");

        return e;

    }

}

这是我的配置文件

<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-3.0.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">

    <context:component-scan base-package="com" />
    <mvc:annotation-driven />

     <bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" id="tilesConfigurer">
       <property name="definitions">
         <list>
           <value>/WEB-INF/tiles.xml</value>
         </list>
       </property>
     </bean>

     <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="viewResolver">
       <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
     </bean> 

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>




</beans>

我呼叫为http://localhost:8080/SpringMVC/test,但收到错误406

3 个答案:

答案 0 :(得分:3)

您可以尝试按照以下方式生成JSON Rsponse。您需要Gson库,controller看起来如下:

@Controller
public class LoginRestCtrl {
    @RequestMapping(value = "/test", method=RequestMethod.GET)
    @ResponseBody
    public  String checklogin(){
        Employee e=new Employee();
        e.setEmailId("sdosi@cur.com");
        e.setId(1);
        e.setPassword("a");
        Gson gson=new Gson();
        String jsonResponse=gson.toJson(e);
        return jsonResponse;

    }

}

Json回复看起来如下:

{"emailId":"sdosi@cur.com","id":1,"password":"a"}

答案 1 :(得分:2)

我建议将jackson依赖项放在你的pom.xml中,如果它们还没有。

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

如果它们已经存在,您可以使用

更新RequestMapping
 @RequestMapping(value = "/test", method=RequestMethod.GET, produces = "application/json;charset=utf-8")

这将确保我们返回带有json标头的响应。

答案 2 :(得分:2)

在pom.xml文件中添加以下依赖项

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.12</version>
</dependency>
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20090211</version>
</dependency>

使用spring配置xml文件添加以下bean

<bean id="jacksonMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jacksonMessageConverter" />
            </list>
        </property>
    </bean>

现在您可以按原样使用控制器

@Controller
public class LoginRestCtrl {
    @RequestMapping(value = "/test", method=RequestMethod.GET)
    @ResponseBody
    public Employee checklogin() {
        Employee e=new Employee();
        e.setEmailId("sdosi@cur.com");
        e.setId(1);
        e.setPassword("a");

        return e;
    }
}