如何解决这个springmvc / json响应错误

时间:2014-11-12 12:36:15

标签: java json spring rest spring-mvc

如何在spring应用程序中编写REST api?我收到了这个错误:

  

此请求标识的资源只能生成   根据请求具有不可接受的特征的回复   "接受"头。

我尝试了很多。我的代码如下所示:

MVC-调度-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-3.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <context:component-scan base-package="com.mkyong.common.controller" />

    <mvc:annotation-driven />

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

</beans>

的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>/movie/*</url-pattern>
    </servlet-mapping>


</web-app>

控制器类文件

package com.mkyong.common.controller;

import java.util.ArrayList;
import java.util.List;

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 com.mkyong.common.model.UserBean;

@Controller
@RequestMapping("/movie")
public class MovieController {


    @RequestMapping(value="/response", method = RequestMethod.GET)
    public @ResponseBody List<UserBean> getMovie() {

        UserBean bean = new UserBean();
        List<UserBean> list = new ArrayList<UserBean>();
        bean.setId("xx");
        bean.setFirstname("xxx");
        bean.setLastname("xxx");
        bean.setSal("xxxx");
        list.add(bean);
        return list;

    }

}

UserBean类

package com.mkyong.common.model;

public class UserBean {

    private String id;
    private String firstname;
    private String lastname;
    private String sal;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    public String getSal() {
        return sal;
    }
    public void setSal(String sal) {
        this.sal = sal;
    }
} 

3 个答案:

答案 0 :(得分:0)

Spring无法将对象转换为JSON。确保您的类路径中有jackson-databind。如果您使用的是Maven,请将其添加到pom.xml

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

答案 1 :(得分:0)

您可以通过在控制器上使用@RequestMapping的“consume”和“produce”属性来指定请求/响应的映射类型。例如,如果您在json中发送请求,并且您还在json中收到响应,则应使用

@RequestMapping(value="/response", method = RequestMethod.GET, consumes = "application/json", produces = "application/json" )

检查您的请求:

  • 您的请求的“Content-Type”标题应与该标题匹配 在RequestMapping的“consumemes”属性中指定。
  • “接受” 您的请求的标头应与中指定的标头匹配 RequestMapping的“生产”属性。

您的错误是指“接受”标题,因此我认为问题是您需要指定“生产”属性

答案 2 :(得分:0)

最后,这位内容谈判代表工作了:

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />