Is it possible to forward a request changing the r

2019-05-21 04:14发布

问题:

I'm working thorough a gateway, which allows only GET requests, whilst REST endpoints behind it are able to accept the wide range of methods (POST, PUT, DELETE, OPTIONS). Therefore, I'm trying to pass the request method as a parameter, having a filter, which forwards the request with a correct method. From what I can see in the specification, it's only allowed to forward the request w/o any modifications:

request.getRequestDispatcher(route).forward(request, response)

Are there any workarounds?

NOTE: Redirect is not an option for me.

回答1:

If you have a single Rest servlet that handles the restful services (and this is usually the case), you can extend it and override the service method. There you can invoke doPost(..), doPut(..), etc. depending on the parameter you want. The default implementation of HttpServlet uses request.getMethod().

Another thing you can do (less preferable) is to have your Filter fire a new request to the endpoint, using URL.openConnection (or apache commons http components), and stream the result of that internal request back to the client. There you can specify the request method.

Anyway, I think you should try to overcome the limitation of your gateway, because it puts you in a really awkward situation.