简体   繁体   English

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

[英]How to solve this springmvc / json response error

How to write REST api in spring application? 如何在Spring应用程序中编写REST API? I am getting this error: 我收到此错误:

The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers. 由该请求标识的资源仅能够生成具有根据请求“接受”标头不可接受的特征的响应。

I tried a lot. 我试了很多 My code looks like this: 我的代码如下所示:

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

Controller class file 控制器类文件

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

Spring is unable to convert the object to JSON. Spring无法将对象转换为JSON。 Make sure you have jackson-databind in your classpath. 确保您的类路径中有jackson-databind If you're using Maven, add this to your pom.xml : 如果您使用的是Maven,请将其添加到pom.xml

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

You can specify what are the mapping types of requests/responses by using the "consumes" and "produces" property of @RequestMapping on your controller. 您可以使用控制器上@RequestMapping的“ consumes”和“ produces”属性来指定请求/响应的映射类型。 Eg if you send the request in json and you also receive the response in json you should use 例如,如果您在json中发送请求,并且在json中也收到响应,则应使用

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

Check your request: 检查您的要求:

  • the "Content-Type" header of your request should match the one specified in the RequestMapping's "consumes" property. 您请求的“ Content-Type”标头应与RequestMapping的“ consumes”属性中指定的标头匹配。
  • The "accept" header of your request should match the one specified in the RequestMapping's "produces" property. 您请求的“ accept”标头应与RequestMapping的“ produces”属性中指定的标头匹配。

Your error refers to the "accept" header, so I assume that the problem is that you need to specify the "produces" property 您的错误是指“ accept”标头,因此我认为问题是您需要指定“ produces”属性

最终,这个内容协商者工作了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM