user2304483
user2304483

Reputation: 1644

How to implement put request correctly in spring boot?

I have basic question about rest controller in spring boot. Example to explain the question: I have the following code from angular:

updateMyEntity(myEntityTemp: MyEntity) {
    let imyEntityObj = {
      myEntity: myEntityTemp
    };    
    return this.http.put(`${AppComponent.ENTITY_API}/Update`, JSON.stringify(imyEntityObj), httpOptions);

My question is about the syntax of the rest controller, here it is:

 @PutMapping("/Update")     
    public MyEntity updateMyEntity(@RequestBody MyEntity myEntity) {    
     }

Is the @RequestBody annotation required? I mean, it won't work without it?

Upvotes: 0

Views: 397

Answers (2)

Rahman786
Rahman786

Reputation: 81

@RestController = @Controller + @ResponseBody

But @RequestBody is still needed if you have some data in the request body to be processed. If path variable is enough for your operation then we don’t need @RequestBody.

Per your posted code, you want to process myEntity then you need it.

Upvotes: 0

S. Anushan
S. Anushan

Reputation: 788

If you are using @Controller and you have returne any String (not view) then you have add @RequestBody.

If you are using @RestController then no need.

Upvotes: 1

Related Questions