Yuval
Yuval

Reputation: 834

Migrating @ResponseBody to ResponseEntity

I have an old controller with a method that returns an object as json and marked with @ResponseBody.

The problem is that the status code is always 200, although sometimes it isn't.

The current method signature is:

public MyResponse myMethod(){}

and I want to change it to:

public ResponseEntity<MyResponse> myMethod(){}

All of that is not a problem, but I afraid that it clients that use this controller will fail to parse the response. Is that possible? do Spring wraps the response with another object?

Upvotes: 0

Views: 1195

Answers (2)

Jovo Skorupan
Jovo Skorupan

Reputation: 267

Controller will return http 200 if it executes without exception. If you return ResponseEntity object with HttpStatus.OK it should work.

Upvotes: 0

twinklehawk
twinklehawk

Reputation: 639

According to the javadoc for @RequestMapping (https://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html), the body of the ResponseEntity will be unwrapped and written to the response output stream. So the output should be the same.

Documentation for the current release (https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-return-types) says essentially the same thing.

Upvotes: 3

Related Questions