Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
spring4restcontroller&;返回JSON_Json_Spring_Rest_Controller_Jackson - Fatal编程技术网

spring4restcontroller&;返回JSON

spring4restcontroller&;返回JSON,json,spring,rest,controller,jackson,Json,Spring,Rest,Controller,Jackson,我有一个简单的控制器,它应该返回JSON,但没有返回。JSON库是Jackson,配置为maven依赖项。当我使用邮递员针对这个url路径发出请求时,我收到一个404错误。当我尝试检查返回的JSON时,我看到“格式错误的JSON:Unexpected”您应该尝试以下操作: @RestController @RequestMapping("/World") public class RestfulController { @RequestMapping(value = "/Country

我有一个简单的控制器,它应该返回JSON,但没有返回。JSON库是Jackson,配置为maven依赖项。当我使用邮递员针对这个url路径发出请求时,我收到一个404错误。当我尝试检查返回的JSON时,我看到“格式错误的JSON:Unexpected”您应该尝试以下操作:

@RestController
@RequestMapping("/World")
public class RestfulController {

    @RequestMapping(value = "/Country", method = RequestMethod.GET)
    public Country findAllCountrys(){
        Country c = new Country(1, "Ethiopia", "Addis Abba", "94 Million");
        return c;
    }  
}
请求url:


这应该返回一个国家的json

如果它不能序列化为json,它将返回一个500内部服务器错误,而不是404。您的请求映射或配置有问题。基于这些映射,您应该将
GET
请求发送到
/World/Country/
,而不是
/World/Country
,基本上是那些跟踪g斜杠不是好主意谢谢阿里,我已经删除了后面的斜杠,但结果仍然一样。我收到一个错误“格式错误的JSON:Unexpected”,现在将您的完整堆栈跟踪添加到您的问题hi availd,谢谢您的回复。我也尝试过这种方法,但没有效果。奇怪的是,该方法似乎返回text/html,如响应标题:Content-Language中所示→ en内容长度→ 987内容类型→ text/html;字符集=utf-8日期→ 2016年1月11日星期一00:24:02 GMT服务器→ ApacheCoote/1.1您需要再添加一件我忘记的事情@requestMapping下方法的ResponseBody。你能试试吗?你好,谢谢你的建议。当我使用@RestController注释时,这应该结合Controller和ResponseBody,所以这一步现在已经过时了。谢谢,如果您只是返回字符串“Hello world”?输出是什么。
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd">


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

<mvc:annotation-driven/>

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json" />
        </map>
    </property>
</bean>
@RestController
@RequestMapping("/World")
public class RestfulController {

    @RequestMapping(value = "/Country", method = RequestMethod.GET)
    public Country findAllCountrys(){
        Country c = new Country(1, "Ethiopia", "Addis Abba", "94 Million");
        return c;
    }  
}