У меня есть простой контроллер, который должен возвращать JSON, но не может этого сделать. Библиотека JSON - это Джексон, настроенный как зависимость от maven. Когда я делаю запрос с использованием почтальона, против этого URL-адреса, я получаю ошибку 404. Когда я пытаюсь проверить JSON Returned, я вижу «Malformed JSON: Unexpected» < ».Spring 4 RestController & Returning JSON
Может ли кто-нибудь сказать, что мне не хватает/не понял? Спасибо
@RestController
@RequestMapping("/World/")
public class RestfulController {
@RequestMapping(value = "/Country/", method = RequestMethod.GET, produces="application/json")
public ResponseEntity<Country> findAllCountrys(){
Country c = new Country(1, "Ethiopia", "Addis Abba", "94 Million");
return new ResponseEntity<Country>(c, HttpStatus.OK);
}
}
контекст весна применение
<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>
Если он не может сериализоваться в json, он вернет 500 Internal Server Error, а не 404. Что-то не так с вашими сопоставлениями или конфигурациями запросов. –
На основе тезисов вам следует отправить 'GET', запросы на '/ World/Country /', даже '/ World/Country' –
В принципе, эти трейлинг-косые черты - не очень хорошие идеи –