I'm currently using Java and Spring for my web service applications. I'm using the @RestController
annotation hoping to remove the need to use the @ResponseBody
and @RequestBody
annotations. Unfortunately, removing the @RequestBody
annotations makes the serialization to fail.
Here's my code that doesn't map the request body to my method parameter that doesn't :
@RestController
@RequestMapping(value = "/member", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class MemberController {
@Autowired
private MemberService memberService;
@RequestMapping("/create")
public void create(Member member) {
memberService.create(member);
}
@RequestMapping("/read")
public Member read(Member member) {
return memberService.read(member);
}
@RequestMapping("/update")
public void update(Member member) {
memberService.update(member);
}
@RequestMapping("/delete")
public void delete(Member member) {
memberService.delete(member);
}
@RequestMapping("/retrieveById")
public Member retrieveById(Member member) {
return memberService.retrieveById(member);
}
@RequestMapping("/retrieveAll")
public List<Member> retrieveAll(Member member) {
return memberService.retrieveAll();
}
}
Do I really need to use the @RequestBody
annotation when I'm already using the @RestController
?
Remember, we don't need to annotate the @RestController-annotated controllers with the @ResponseBody annotation since Spring does it by default.
requestBody consists of the content object, an optional Markdown-formatted description , and an optional required flag ( false by default). content lists the media types consumed by the operation (such as application/json ) and specifies the schema for each media type. Request bodies are optional by default.
In brief, the @RequestBody annotation is responsible for retrieving the request body and automatically converting it to the Java object. Let's have an example to explain this. So let's create the REST controller to accept the student registration. So there is no need to have converted the HTTP request to an object.
Annotation Type RequestBody Annotation indicating a method parameter should be bound to the body of the web request. The body of the request is passed through an HttpMessageConverter to resolve the method argument depending on the content type of the request.
@RestController
contains @ResponseBody
so you do not need this any more.
But you still need the @RequestBody
annotation, because the method you call for a POST request might contain more than one parameters, one of which is mapped to the request body, other parameters of the method might be PathVariables
or for example a UriComponentsBuilder
.
And to mark the parameter which is to be mapped to the request body, you need the annotation.
UPDATED: Yes you still need the @RequestBody because you are doing a POST. If you change for a get you don't really need it. As you are trying to do a REST API you should try to use the HTTP status code.
GET for requesting information. POST or PUT for creating/updateing information DELETE for deleting information.
According to the documentation you don't need that to write @ResponseBody
For convenience, instead of annotating all your @RequestMapping methods with @ResponseBody, you can annotate your controller Class with @RestController.
@RestController is a stereotype annotation that combines @ResponseBody and @Controller. More than that, it gives more meaning to your Controller and also may carry additional semantics in future releases of the framework.
I think you should remove the method = RequestMethod.POST from your class and add it on each method. retrieveAll method should be a GET instead of a POST.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With