HttpServletRequest getParameter AND getReader

I am developing a HttpServletone that processes a POST request from an application that includes query string parameters, and a JSON object in the body. This is not a form post, the whole body is a JSON object. I understand what I need to use HttpServletRequest.getReader()to read the body. But I also need to use HttpServletRequest.getParameter()to get query parameters. And I understand that both of them cannot be used.

I think the proposed solution is to create HttpServletRequestWrapperand redefine getReader()in such a way that it is called more than once. But I can’t figure out how to do this. Or perhaps this is not the intended approach. All the examples HttpServletRequestWrapperthat I can find seem to be related to creating filters and modifying the contents of the request.

Any help is greatly appreciated.

By the way, this is hosted by Google App Engine, but I don’t think it will affect the decision.

+3
source share
2 answers

I think you can implement multi-valued getReader()in your own HttpServletRequestWrapper:

  • save the body of the HTTP request to a temporary file.
  • When called HttpServletRequestWrapper.getReader(), open the temporary file.
  • delete the temporary file at the end of request processing.

to implement (1) and (3), it ServletFiltermay be useful.

+1
source

I would suggest reading query string parameters directly using query string parsing . This way you will not touch the body of the message, and you can use HttpServletRequest.getReader().

, URLEncodedUtils Apache Http Client, :

+1

All Articles