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
Provide better intercepting model for controllers returning @ResponseBody [SPR-10859] #15486
Comments
Michael Osipov commented Isn't it absolutely pointless to repeat the same information again? I don't think that twitter is a good example here. |
Marcel Overdijk commented ~michael-o what point are you trying to make? your comment is completely worthless... |
Michael Osipov commented Your usecase is a bad example because the information in the response is redundant. There is no need to duplicate the response code. |
Marcel Overdijk commented The response code is not duplicated. |
Michael Osipov commented My bad, did not read careful enough. I have encountered such an API before. It remains bad design. This isn't REST because you do abuse the HTTP status codes. |
Jon Brisbin commented This is simply JSONP without the function call. It's not abusing HTTP status codes but ensuring that handlers are always invoked no matter the outcome according to the status, which might be correct in a RESTful situation but can't be dealt with in a purely RESTful way because a browser isn't a RESTful client (it makes certain assumptions in the Ajax APIs that don't always fit well with what's being accomplished). If using JSONP, a similar situation exists where you need to take a string of JSON, change the Content-Type to application/javascript, change the HTTP status code to ensure that the Ajax library will actually invoke the function, and wrap the text with a call to a Javascript function. All this should happen after all the other normal JSON processing. Currently, however, it's hard to do this with anything other than a ServletFilter. |
Marcel Overdijk commented Jon Brisbin Please vote for issue if you think this is important, so it might get attention from from Spring devs. |
Sergey Gluschenko commented The other usecase is that I need to add completely custom headers to response, while controller's methods are returning |
Michael Osipov commented Sergey Gluschenko, why don't you simply use |
Sergey Gluschenko commented Michael Osipov, that's because I'm writing an utility and I don't want to force end users to change their preferred approach of working with Spring MVC. I accept your proposal as a workaround, but I think you'll agree that there is already a number of live usecases that require improvement requested. |
Michael Osipov commented Sergey Gluschenko, yes, I do agree. I have created a similar ticket ages ago, see #14614. |
Sandeep Chouksey commented I do have a use case where we need to add customer header to response and our current approach is based on |
Jason Eacott commented I have a use case where I need to generate response headers where the header content depends on data generated during the request. I'm currently using |
Jason Eacott commented I have a use case where I need to generate response headers where the header content depends on data generated during the request. I'm currently using I also tried this without |
Rossen Stoyanchev commented
I'm not sure I understand exactly what you mean but if you haven't yet, check
Actually we don't do that, at least not explicitly. There were some converters that would cause the OutputStream to be closed but those were fixed (see this commit). Keep in mind however that when the content written to the response exceeds the Servlet container output buffer, at that point the response is committed. So waiting until after the content has been written isn't necessarily going to work and any additional "post"-writing hooks we provide through this ticket isn't going to change that . |
Jon Brisbin commented I had to do something like this for JSONP support. It basically had to always return a 200 no matter what happened in the |
Jason commented I ended up using an Aspect on my controller methods to modify all my responses. |
Rossen Stoyanchev commented Support for this has been added in the master branch. See commit 96b18c that adds Update May 30, 2014: |
Rossen Stoyanchev commented Marking as resolved for now but this is very much still open for further refinements based on feedback! |
Rossen Stoyanchev commented I've also added a JSONP |
Ken Wang commented Hey Rossen, first off awesome work on this piece! It's perfect for my needs of wrapping the return values from my Controllers. However, one small thing preventing me from making proper use of it is that the Advice is not triggered when the return value is null. I opened an issue about this #16766. It would be great if you can take a look when you have time. Thanks. |
Marcel Overdijk commented Nice to see this feature. |
Rossen Stoyanchev commented Thanks and thanks for suggesting the idea! Yes you have a point about the name. It is meant as a reference to |
Marcel Overdijk opened SPR-10859 and commented
I'm using Spring as rest server where all controller actions return
@ResponseBody
annotated results. Just like Twitter I'm implementing 'suppress_response_codes' functionality.Normally I will return json response like:
But with a
suppress_response_codes=true
query param in the request it is:Implementing a HandlerInterceptor does not seem the best fit for intercepting controller actions returning
@ResponseBody
responsed as the converter seems to have already send the data in the postHandle.There should be decent intercepting model for
@ResponseBody
where it's possible to retrieve the original object being returned by the controller actions.Affects: 3.2.4
Issue Links:
@RequestMapping
methods ("is duplicated by")@JsonView
@ResponseBody
("supersedes")Referenced from: commits c9d0ebd, 96b18c8, 2655c50
6 votes, 12 watchers
The text was updated successfully, but these errors were encountered: