I am new in writing spring rest controller. I want to create a simple sending in JSON format, but I can't deal with it, is my code a correct code for it?
In my code, there is a RestTemplate, which sends a simple POJO to the REST url, and the REST Controller sends back another POJO.
I found a lot of examples sending and receiving objects as JSON, but some of them are a few years old. The most I found is where they configure the dispatcher bean XML by adding MappingJackson2HttpMessageConverter
for Configure bean to convert JSON to POJO and vice versa :
...
<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter"/>
</beans:list>
</beans:property>
</beans:bean>
<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>
...
And also they set it to the RestTemplate java code:
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new MappingJackson2HttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
Sometimes they set a header too:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Accept", "application/json");
HttpEntity<MyObject> entity = new HttpEntity<MyObject>(inputRequest, headers);
Sometimes they convert their object to JSON format, and send it as a text, not the instance.
And also I can found 2 ways for sending a POST message:
ResponseEntity<MyObject> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, MyObject.class);
or
MyObject response = restTemplate.postForObject(url, inputRequest, MyObject.class);
And this is the REST Controller:
@RequestMapping(value = "/send", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public MyObject send(@RequestBody MyObject requestModel) {
return //whatever;
}
BUT: if I don't set anything into the XML, and don't add any message converter and header to the RestTemplate, it looks like it working fine. I test it by PostMan, and if I add a JSON format of my MyClass example, I receive JSON.
So my question is: Is my code correct this way for JSON sending really?:
mvc-dispatcher.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- to reach resources like css and images -->
<default-servlet-handler/>
<!-- the REST controllers are here -->
<context:component-scan base-package="hu.viktor" />
</beans:beans>
RestTemplate java:
@Controller
public class RequestSender {
public MyObject send(MyObject inputRequest) {
RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8080/rest/send";
MyObject response = restTemplate.postForObject(url, inputRequest, MyObject.class);
return response;
}
}
And the REST Controller:
@Controller
@RequestMapping("/rest")
public class CalculatorRestController {
@RequestMapping(value = "/send", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public MyObject send(@RequestBody MyObject requestModel) {
return //whatever;
}
}
According to documentation RequestMappingHandlerMapping
and RequestMappingHandlerAdapter
will be added automatically, if you specified <mvc:annotation-driven/>
in xml configuration (in your case just <annotation-driven>
). The same documentation contains the list of HttpMessageConverters
, which will be set up by <mvc:annotation-driven>'
. And:
MappingJackson2HttpMessageConverter
converts to/from JSON — added if Jackson 2 is present on the classpath.
This means, that you don't need to add json message converter manually, just specify <mvc:annotation-driven>
in configuration file and declare dependencies in your pom.xml (if you're using Maven):
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.6.1</version>
</dependency>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.