Spring MVC @RequestBody @ResponseBody Example
TP request/response body with a domain object in method parameter or return type. Behind the scenes, these annotation usesHTTP Message converters to convert the body of HTTP request/response to domain objects.
@RequestBody
Take this method for example:
@RequestMapping(value="/user/create", method=RequestMethod.POST)
publicResponseEntity<Void> createUser(@RequestBodyUser user, UriComponentsBuilder ucBuilder){
System.out.println("Creating User "+user.getName());
if(userService.isUserExist(user)){
System.out.println("A User with name "+user.getName()+" already exist");
returnnew ResponseEntity<Void>(HttpStatus.CONFLICT);
}
userService.saveUser(user);
HttpHeaders headers =new HttpHeaders();
headers.setLocation(ucBuilder.path("/user/{id}").buildAndExpand(user.getId()).toUri());
returnnew ResponseEntity<Void>(headers, HttpStatus.CREATED);
}
This is the controller method to handle typical HTTP Post request [for URL /user/create]. In a pure REST oriented approach, this controller method creates a user, and returns the HTTP 201[CREATED] along with a LocationHeader containing the locations of newly created user [/app-address/user/1 e.g].
Now back to our original discussion, HTTP Post request body contains the detail of user to be created. When a client sends a request [/user/create] to create a user, it will be intercepted in this method. Method parameteruser is marked with @RequestBody annotation. Thanks to this annotation, Spring will try to bind the request body [which can be JSON/XML/Other] to user object[ Means crating a new user object with the details found in the request body like user name,age etc..], based on Content-Type header in Http request.
But Spring need help to convert the request body into user object. It needs a converter which can convert the data in HTTP request body [which can be JSON/XML/Other] into user object.
Spring provides out-of-box many default HttpMessageConverters, which will be used for conversion, depending on presence of certain library in project classpath.
For example, if the Content-Type in request Header was one of application/json or application/xml , that means the POST body contains json or XML[Popular formats], and if Jackson library is found in your classpath, Spring will delegate the conversion to MappingJackson2HttpMessageConverter [for json] or MappingJackson2XmlHttpMessageConverter [for xml].
To declare a dependency to Jackson library (jackson-databind) include following dependency in your pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
ResponseEntity (used in above example) represents the entire HTTP response. Good thing about it is that you can control anything that goes into it. You can specify status code, headers, and body.Next post goes into details of it with a fully working example.
@ResponseBody
If a method is annotated with @ResponseBody, Spring will bind the return value to outgoing HTTP response body. While doing that, Spring will [behind the scenes] use HTTP Message converters to convert the return value to HTTP response body [serialize the object to response body], based onContent-Type present in request HTTP header.
Take this method for example:
@RequestMapping(value ="/user/all", method = RequestMethod.GET)
public@ResponseBody List<User> listAllUsers() {
returnuserService.findAllUsers();
}
This is the controller method to handle typical HTTP GET request [for URL /user/all] to retrieve all users. In this case, Spring will convert the user list into appropriate format [JSON/XML/Other] using available converters, based on content type.
NOTE : As from Spring 4, @RestController is the preferred way to achieve the same functionality earlier provided by @ResponseBody. Under the hood, @RestController is @Controller+@ResponseBody, and it avoids the need of prefixing every method with @ResponseBody. Next post goes into details with a full working example.
Default HttpMessageConverters
Spring provides out of box following Http message converters which implements HttpMessageConverter interface [Credit : Spring Reference].
Custom HttpMessageConverters
Most of the time, the default converters provided by Spring are enough. But if you need some custom behavior, you can roll out your own implementaion.
For instance, in above example, MappingJackson2HttpMessageConverter was used to handle JSON content. By default, the Jackson ObjectMapper supplied with this converter fails if there are missing properties in your JSON or domain object. It simply fails to convert. You can override this behavior by telling object mapper not to fail on missing properties, as shown below:
packagecom.websystique.springmvc.configuration;
importjava.util.List;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.ComponentScan;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.http.converter.HttpMessageConverter;
importorg.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
importorg.springframework.web.servlet.config.annotation.EnableWebMvc;
importorg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
importcom.fasterxml.jackson.databind.DeserializationFeature;
importcom.fasterxml.jackson.databind.ObjectMapper;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages ="com.websystique.springmvc")
publicclass HelloWorldConfigurationextends WebMvcConfigurerAdapter{
@Override
publicvoid extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(mappingJackson2HttpMessageConverter());
}
@Bean
publicMappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter =new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(newObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false));
returnconverter;
}
}
Here we have overridden extendMessageConverters method which provides a hook to add your own validator, wihtout skipping all existing validators.
That’s it about the basic introduction on REST. Next post shows a RESTFUL CRUD web service, complete example with HTTP GET,PUT,POST & DELETE methods mapping.
ref link : http://websystique.com/springmvc/spring-mvc-requestbody-responsebody-example/
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.