简体   繁体   中英

How to solve this springmvc / json response error

How to write REST api in spring application? 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

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

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

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. Make sure you have jackson-databind in your classpath. If you're using Maven, add this to your 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. Eg if you send the request in json and you also receive the response in json you should use

@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.
  • The "accept" header of your request should match the one specified in the RequestMapping's "produces" property.

Your error refers to the "accept" header, so I assume that the problem is that you need to specify the "produces" property

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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