Skip to content
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

ResponseBodyAdvice to accept null values? [SPR-12152] #16766

Closed
spring-projects-issues opened this issue Sep 4, 2014 · 6 comments
Closed

ResponseBodyAdvice to accept null values? [SPR-12152] #16766

spring-projects-issues opened this issue Sep 4, 2014 · 6 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 Sep 4, 2014

Ken Wang opened SPR-12152 and commented

ResponseBodyAdvice is amazing for my needs except for one thing, it doesn't seem to be triggered when the ResponseBody returns a null value.

Is it possible to handle this case through the ResponseBodyAdvice?


Affects: 4.1 GA

Issue Links:

0 votes, 5 watchers

@spring-projects-issues
Copy link
Collaborator Author

Ken Wang commented

So I looked through the source code and found the deciding piece in the function RequestResponseBodyMethodProcessor.handleReturnValue(). In this function there is a null check that only runs AbstractMessageConverterMethodProcessor.writeWithMessageConverters() if returnValue is not null.

As far as I can tell by following the call chain, this returnValue is only required to be nonnull by the function AbstractMessageConverterMethodProcessor.writeWithMessageConverters() in which returnValue.getClass() is called.

However, if I understand it properly, this value can be obtained by returnType.getMethod().getReturnType().

So ideally, I'd like to get rid of this null check in RequestResponseBodyMethodProcessor.handleReturnValue(). Would this be a viable solution?

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

I'll take a closer look, thanks.

@spring-projects-issues
Copy link
Collaborator Author

Ken Wang commented

Thanks man, much appreciated.

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

I've added a fix. It turned out to be a little more complex since the return type could be ResponseEntity<?> in which case we've to obtain the generic parameter type.

@spring-projects-issues
Copy link
Collaborator Author

Ken Wang commented

Thank you for the fix! I hadn't thought about ResponseEntity, hope that wasn't too much of a hurdle.

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

No worries. Thanks for the suggestion.

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