Nikki Nicholas Romero
Nikki Nicholas Romero

Reputation: 287

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

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?

Upvotes: 8

Views: 5455

Answers (3)

Dinakar N
Dinakar N

Reputation: 31

@RestController
@RequestMapping("/test")
public class TestController {

    private static final Logger logger = LoggerFactory.getLogger(TestController.class);

    @PostMapping(path = "/insert", consumes = "application/json", produces = "application/json")
    
    public void insertMethod(@RequestBody TestEntity t) {
        testService.insertData(t);
    }

If you don't add @RequestBody it will insert null values (should use), no need to use @ResponseBody since it's part of @RestController.

Upvotes: 1

Abacus
Abacus

Reputation: 19421

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

Upvotes: 12

Henrique Miranda
Henrique Miranda

Reputation: 994

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.

Upvotes: 1

Related Questions