是否可以转发更改请求方法的请求? [英] Is it possible to forward a request changing the request method?

查看:172
本文介绍了是否可以转发更改请求方法的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过一个网关,它只允许GET请求,而它后面的REST端点能够接受各种方法(POST,PUT,DELETE,OPTIONS)。因此,我试图将请求方法作为参数传递,具有过滤器,该过滤器使用正确的方法转发请求。从我在规范中可以看到,它只允许转发请求没有任何修改:

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)

有没有解决方法?

注意:重定向对我来说不是一个选项。

NOTE: Redirect is not an option for me.

推荐答案

如果你有一个Rest servlet来处理restful服务(通常就是这种情况),你可以扩展它并覆盖 service 方法。在那里你可以调用 doPost(..) doPut(..)等等,具体取决于你想要的参数。 HttpServlet 的默认实现使用 request.getMethod()

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().

您可以做的另一件事(不太可取)是让您的过滤器使用 URL.openConnection (或apache commons http组件)向端点发出新请求,并将该内部请求的结果传回客户端。在那里你可以指定请求方法。

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.

这篇关于是否可以转发更改请求方法的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆