Spring MVC 4.1.x RestFul 服务抛出对于 Json 不可接受

Spring MVC 4.1.x RestFul Service throwing Not Acceptable for Json

我使用 Spring MVC4.1.X 做了一个休息服务,但是每当我尝试 return Json o/p 到浏览器时,我都会得到以下错误

此请求标识的资源只能根据请求生成具有不可接受的特征的响应"accept" headers.

我正在使用@Response body 将 Java pojo 自动转换为 JSON objects。 Whosebug 上给出的几乎所有解决方案我都试过了。

my controller class 
package com.spring.rest.ambulance;

import java.io.IOException;

import net.sf.json.JSONObject;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.spring.dao.AmbulanceDAO;
import com.spring.dao.AmbulanceDAOImpl;
import com.spring.model.Ambulance;


@RestController
@RequestMapping("/Ambulance")
public class AmbulanceRestController {
    @Autowired
    private AmbulanceDAO ambulanceDAO;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public @ResponseBody String getAllUsers(ModelMap model) {
        String jsonData = "[{\"id\":\"3253123\",\"firstname\":\"Chris\",\"lastname\":\"Johnson\",\"address\":\"211, Geoffrey Drive\",\"city\":\"Newark\",\"phone\":\"999-888-6666\",\"email\":\"chrisj@yahoo.com\"},{\"id\":\"67643837\",\"firstname\":\"Bill\",\"lastname\":\"Derkson\",\"address\":\"201, Sleepy Hollow Drive\",\"city\":\"Newark\",\"phone\":\"999-777-2222\",\"email\":\"billd@gmail.com\"}]";
        return jsonData;
    }

    @RequestMapping(value = "/{id}")
    public @ResponseBody Ambulance getAmbulanceProviders(ModelMap model,
            @PathVariable("id") int Id) {
        String jsonData = null ;
        Ambulance ambulance = ambulanceDAO.getById(Id);
        ObjectMapper objmapper = new ObjectMapper();
        try {
             jsonData = objmapper.writeValueAsString(ambulance);
            //System.out.println(objmapper.writeValueAsString(ambulance));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ambulance;
    }

Entity being returned 
package com.spring.model;

import org.codehaus.jackson.map.ObjectMapper;

public class Ambulance {

    private int ID;
    private String vehicleNumber;
    private String ambulanceType;
    private String ambulanceProviderName;
    /**
     * @return the iD
     */
    public int getID() {

        return ID;
    }

    /**
     * @param iD
     *            the iD to set
     */
    public void setID(int iD) {
        ID = iD;
    }


    /**
     * @return the vehicleNumber
     */
    public String getVehicleNumber() {
        return vehicleNumber;
    }

    /**
     * @param vehicleNumber
     *            the vehicleNumber to set
     */
    public void setVehicleNumber(String vehicleNumber) {
        this.vehicleNumber = vehicleNumber;
    }

    /**
     * @return the ambulanceType
     */
    public String getAmbulanceType() {
        return ambulanceType;
    }

    /**
     * @param ambulanceType
     *            the ambulanceType to set
     */
    public void setAmbulanceType(String ambulanceType) {
        this.ambulanceType = ambulanceType;
    }

    /**
     * @return the ambulanceProviderName
     */
    public String getAmbulanceProviderName() {
        return ambulanceProviderName;
    }

    /**
     * @param ambulanceProviderName
     *            the ambulanceProviderName to set
     */
    public void setAmbulanceProviderName(String ambulanceProviderName) {
        this.ambulanceProviderName = ambulanceProviderName;
    }
    @Override
    public String toString() {
        return "{ID="+ID+",AmbulanceProvderName="+ambulanceProviderName+",AmbulanceType="+ambulanceType+",VehicleNumber="+vehicleNumber+"}";
    }

}

    DAO Ambulance get by Id
    public Ambulance getById(int id) {
        // TODO Auto-generated method stub
        Ambulance ambulance = new Ambulance(); 
    /*  JdbcTemplate jdbcTemplate =  new JdbcTemplate();
        System.out.println(dataSource);
        jdbcTemplate.setDataSource(dataSource);*/
        System.out.println(jdbcTemplate.getDataSource());
        String sql = "SELECT * FROM AMBULANCE WHERE AMBULANCEID = ?";
        ambulance = (Ambulance)jdbcTemplate.queryForObject(sql,new Object[] { id }, new AmbulanceRowMapper());


        return ambulance;
    }


pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>SpringWebAppRestServices</groupId>
    <artifactId>SpringWebAppRestServices</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.5.0</version>
</dependency>
<dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.8.5</version>
    </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>

    </dependencies>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

最初,当请求发送的接受 header 与响应的 content-type 不同时,会出现此问题。我不知道是不是这样,没有提供足够的来源。

但是,当发生相同错误时,另一种更棘手的情况是框架无法将响应转换为适当的表示形式,例如由于 getters/setters 错误或缺少依赖项。

在您发布的代码中,我发现您的依赖项存在问题。 Spring 4.1 needs minimum Jackson 2.1,在 Jackson 2 及更高版本中,包从 codehaus 更改为 fasterxml。将您的两个 Jackson 依赖项替换为以下一个

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

或者更好的是去更新版本。单一依赖就足够了,它会传递 com.fasterxml.jackson.core:jackson-annotations:jarcom.fasterxml.jackson.core:jackson-core:jar

我认为您可能必须确保您的请求在 header 中包含以下内容:

GET 
Accept: application/json