Skip to content

@ResponseBody [SPR-5766] #10436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue May 18, 2009 · 7 comments
Closed

@ResponseBody [SPR-5766] #10436

spring-projects-issues opened this issue May 18, 2009 · 7 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented May 18, 2009

Kenny MacLeod opened SPR-5766 and commented

It seems odd and undesirable that the annotation-style of MVC controllers should only make use of HttpMessageConverter on the request, and not the response. Specifically, @RequestBody -annotated arguments can be resolved using HttpMessageConverter .read(), but there's no support for rendering the response using HttpMessageConverter .write().

This is addressed to some degree in #10100, but I'm focussing here more on writing elegant REST controllers; the controller should be able to take the typed and unmarshalled request body Object, and should be able to return a similarly type Object for marshalling. The ModelAndViewResolver approach suggested in #10100 just doesn't seem right.

Would it be possible for the standard return value resolution logic to consult with any HttpMessageConverters in the context, as happens with @RequestBody annotations? There doesn't seem to be a need for a @ResponseBody annotation.


Affects: 3.0 M3

Issue Links:

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

Well, if we are going to use HttpMessageConverters, we do definitely need a explicit annotation to trigger it. This is due to the fact that @Controllers can return a String or 'simple value type' (URL, Date, Number, Locale, etc), to be interpreted as view name or model object respectively.

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

Edit the title and replaced HttpMethodConverter with HttpMessageConverter.

@spring-projects-issues
Copy link
Collaborator Author

Kenny MacLeod commented

How about changing AnnotationMethodHandlerAdapter so that it can be configured with two sets of HttpMessageConverters, one for @RequestBody annotations, and one for method return values? The latter can be blank by default, and so retains current behaviour, but you could wire it up with a message converter for handling return values, if you so choose.

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

Hmm, I am not a big fan of having two sets of HttpMessageConverters. The idea for the message converters is that they work out of the box for common 'machine-to-machine' REST types, like byte[], XML, JSON (forthcoming), etc. If you write your own converter, it should be usable in both request and response scenario's, on both client and server side. For server-side request/response-specific scenarios we already have the WebArgumentResolver and ModelAndViewResolver.

Another underlying issue is that return type resolution is already quite complex, and on the verge of being ambiguous, considering all the different return type variants you can have, and also considering the fact that the method name can be significant too. This makes the Spring code for handling these quite hard to understand already, and supporting message converters for return types would make it even harder. Unfortunately, refactoring this code is not an option, because we have to maintain backwards compatibility.

Therefore, the main reason for introducing a new, supplemental annotation like @ResponseBody would be to decrease this ambiguity: using this annotation would mean that you use message converters, and not the defaults.

I know this is not a very strong argument: it mostly comes down to a matter of taste, and Spring Framework internals. But I do think having a separate @ResponseBody annotation is the best way forward.

@spring-projects-issues
Copy link
Collaborator Author

Kenny MacLeod commented

OK, I'm cool with that. Perhaps, for consistency with @ResponseStatus, the @ResponseBody annotation could have attributes which indicate where the response body should be taken from, specified as an enum, defaulting to the method return value. I'm not sure what other possibilities there would be, mind you.

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

Done!

@spring-projects-issues
Copy link
Collaborator Author

Kenny MacLeod commented

Great stuff. I see that the annotation is defined as only allowed on methods... could it be allowed on the controller class also, indicating that all methods "inherit" it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants