We have to use ResponseEntity or use @ResponseStatus to give the correct status code. But this could be linked to the type of the HTTP METHOD (GET, POST ..) so that we would not need to put more code.

Comment From: philwebb

The @ResponseStatus annotation is part of Spring Framework. I'll transfer your issue for them to consider. I suspect that they may not want to add an additional attribute on @ResponseStatus given that it will then provide a third way of setting the status code.

Comment From: sbrannen

But this could be linked to the type of the request (GET, POST ..) so that we would not need to put more code.

What do you mean by "linked" in the above sentence?

In other words, what are you explicitly proposing?

Comment From: GuilhermePetena

If I put @PostMapping automatically when making the POST request, the status code returned will be 201 CREATED, because the default for all Mapping is to return the status code 200 OK.

Ex: repository.save(entity); By default this would return status code 200 and not 201.

Comment From: sbrannen

OK. So you are proposing that Spring infer a default status code based on the mapped HTTP method.

Comment From: GuilhermePetena

Yes exactly that. Sorry for my explanation, I am learning to speak English.

Comment From: sbrannen

Please note that this is actually a duplicate of #18578 which got bulk closed.

See also the discussion in https://github.com/sbrannen/spring-composed/issues/10.

Comment From: rstoyanchev

I think the key is this has to be done selectively (e.g. @RestController but not necessarily any @Controller) and at this point in time, it probably has to be explicit, e.g. via configuration or a protected method somewhere. Either of those doesn't seem ideal, one would just add more configuration, and the other would require deciding where to apply which status.

One thought is that it's easy to create a custom annotation that combines HTTP POST with 201:

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = {RequestMethod.POST})
@ResponseStatus(HttpStatus.CREATED)
public @interface POST {

    @AliasFor(annotation = RequestMapping.class)
    String[] value() default {};
}
@RestController
public class AccountController {

    @POST("/accounts")
    Account handle(@RequestBody Account account) {
        return ... ;
    }

The advantage of this is that it's flexible enough to apply where needed without being any more verbose.

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: GuilhermePetena

For me, the ideal is that Spring inferred the status code of the post for example, if it is not possible, fine.

Comment From: rstoyanchev

This has been discussed under #18578 as well. The discussion focuses mostly on GET vs POST and does not mention what if anything could be useful for other HTTP methods. For POST the suggestion is to send 201 (CREATED), but 204 (NO_CONTENT) could also be used, and 200 (OK) likewise where the response includes the created entity. There isn't a clear enough cut case for changing long established defaults.