簡體   English   中英

如何解決這個springmvc / json響應錯誤

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

如何在Spring應用程序中編寫REST API? 我收到此錯誤:

由該請求標識的資源僅能夠生成具有根據請求“接受”標頭不可接受的特征的響應。

我試了很多 我的代碼如下所示:

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>

控制器類文件

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

Spring無法將對象轉換為JSON。 確保您的類路徑中有jackson-databind 如果您使用的是Maven,請將其添加到pom.xml

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

您可以使用控制器上@RequestMapping的“ consumes”和“ produces”屬性來指定請求/響應的映射類型。 例如,如果您在json中發送請求,並且在json中也收到響應,則應使用

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

檢查您的要求:

  • 您請求的“ Content-Type”標頭應與RequestMapping的“ consumes”屬性中指定的標頭匹配。
  • 您請求的“ accept”標頭應與RequestMapping的“ produces”屬性中指定的標頭匹配。

您的錯誤是指“ accept”標頭,因此我認為問題是您需要指定“ produces”屬性

最終,這個內容協商者工作了:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM