JSON 来自 SpringMVC 控制器的响应不工作

JSON response from SpringMVC controller is not working

我想从 Spring MVC 控制器响应 JSON 对象或数组。从这两个 beingjavaguys and mkyoung 教程中,我尝试了。 只有当响应是字符串时,我才能成功。但是当响应在对象或列表中时,它对我不起作用。

//It works
@RequestMapping(value = "/angular", method = RequestMethod.GET)
public @ResponseBody String getAllProfiles( ModelMap model ) {
String jsonData = "[{\"firstname\":\"ajitesh\",\"lastname\":\"kumar\",\"address\":\"211/20-B,mgstreet\",\"city\":\"hyderabad\",\"phone\":\"999-888-6666\"},{\"firstname\":\"nidhi\",\"lastname\":\"rai\",\"address\":\"201,mgstreet\",\"city\":\"hyderabad\",\"phone\":\"999-876-5432\"}]";
return jsonData;
}

输出为:

但是问题就在里面,

@RequestMapping(value = "mkyoung", method = RequestMethod.GET)
    public @ResponseBody Shop getShopInJSON() {

        Shop shop = new Shop();
        shop.setName("G");
        shop.setStaffName(new String[] { "mkyong1", "mkyong2" });
        return shop;
    }

显示,

HTTP ERROR 406

Problem accessing /mkyoung.html. Reason:

    Not Acceptable

但是如果我改变它 toString() , 它有效但不适用于正确的输出

@RequestMapping(value = "mkyoung", method = RequestMethod.GET)
public @ResponseBody String getShopInJSON() {

    Shop shop = new Shop();
    shop.setName("G");
    shop.setStaffName(new String[] { "mkyong1", "mkyong2" });
    return shop.toString();

}

但我需要 JSON 对象或对象数组作为响应。什么是问题?我在 pom.xml

中添加了 jsckson 依赖项
<dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.10</version>
    </dependency>

更新: 现在我通过添加

从 angular js 发送请求
headers: {
                "Content-Type": "application/json"
            }

我的调度员-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <mvc:annotation-driven />

    <context:component-scan base-package="com.sublime.np.controller" />

    <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/defs/general.xml</value>
            </list>
        </property>
    </bean>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.tiles3.TilesView" />
    </bean>

</beans>

作为 Sotirios Delimanolis 的回答建议。

$http({
            url: '/mkyoung.html',
            method: 'GET',
            data: id,
            headers: {
                "Content-Type": "application/json"
            }
            }).success(function(response){
                $scope.response = response;
                console.log($scope.response);
               /*  $scope.hideTable = false;
                $scope.hideButton  = false ; */
            }).error(function(error){
                $scope.response = error;
                console.log("Failed");
        });

但它显示相同的错误。

给定 @ResponseBody POJO 类型 return 类型,默认 MVC 配置 @EnableWebMvc<mvc:annotation-driven />,类路径上的 Jackson,Spring将尝试将 POJO 序列化为 JSON 并将其写入响应 body.

由于正在写入 JSON,因此它将尝试将 application/json 写入 Content-type header。 HTTP 规范要求服务器仅响应属于请求中 Accept header 的内容类型。

您发送的请求似乎带有不适当的 Accept header,其中不包含 application/json。解决这个问题。


请注意,您正在将请求发送至

/mkyoung.html

Spring,默认情况下,使用基于某些扩展的内容协商。比如用.html,Spring会认为请求应该产生text/html的内容,这与你要发送的application/json相反

因为您的处理程序已经映射到

@RequestMapping(value = "/mkyoung", method = RequestMethod.GET)

只需将请求发送到相应的URL,以/mkyoung结尾。 (删除 .html 扩展名。)

同时添加jackson核心

<dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>2.5.1</version>
</dependency>

对于列表使用方法return类型Object

@RequestMapping(value = "mkyoung", method = RequestMethod.GET)
    public @ResponseBody Object getShopInJSON() {

        Shop shop = new Shop();
        shop.setName("G");
        shop.setStaffName(new String[] { "mkyong1", "mkyong2" });
        Shop shop1 = new Shop();
        shop1.setName("G1");
        shop1.setStaffName(new String[] { "mkyong1", "mkyong2" });
        List<Shop> shops = new ArrayList<Shop>();
        shops.add(shop1);
        shops.add(shop);
        return shops;
}