Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring : Why should I still use @RequestBody when my class is already annotated with @RestController?

Tags:

java

rest

spring

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?

like image 534
Nikki Nicholas Romero Avatar asked Sep 06 '16 07:09

Nikki Nicholas Romero


People also ask

Is @RequestBody required with @RestController?

Remember, we don't need to annotate the @RestController-annotated controllers with the @ResponseBody annotation since Spring does it by default.

Is @RequestBody mandatory?

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.

Why do we need RequestBody?

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.

What is the purpose of @RequestBody annotation?

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.


2 Answers

@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.

like image 104
P.J.Meisch Avatar answered Sep 23 '22 21:09

P.J.Meisch


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.

like image 20
Henrique Miranda Avatar answered Sep 24 '22 21:09

Henrique Miranda