泽西岛 URL 转发
在 Jersey REST 方法中,我想转发到另一个网站。我怎样才能做到这一点?
@Path("/")
public class News {
@GET
@Produces(MediaType.TEXT_HTML)
@Path("go/{news_id}")
public String getForwardNews(
@PathParam("news_id") String id) throws Exception {
//how can I make here a forward to "http://somesite.com/news/id" (not redirect)?
return "";
}
}
编辑:
尝试执行以下操作时,出现 No thread local value in range for proxy of class $Proxy78
错误:
@Context
HttpServletRequest request;
@Context
HttpServletResponse response;
@Context
ServletContext context;
...
RequestDispatcher dispatcher = context.getRequestDispatcher("url");
dispatcher.forward(request, response);
Inside a Jersey REST method I would like to forward to an another website. How can I achieve that?
@Path("/")
public class News {
@GET
@Produces(MediaType.TEXT_HTML)
@Path("go/{news_id}")
public String getForwardNews(
@PathParam("news_id") String id) throws Exception {
//how can I make here a forward to "http://somesite.com/news/id" (not redirect)?
return "";
}
}
EDIT:
I'm getting the No thread local value in scope for proxy of class $Proxy78
error when trying to do something like this:
@Context
HttpServletRequest request;
@Context
HttpServletResponse response;
@Context
ServletContext context;
...
RequestDispatcher dispatcher = context.getRequestDispatcher("url");
dispatcher.forward(request, response);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在我的应用程序中使用了上面的代码并且它有效。
I used above code in my application and it works.
试试这个,它对我有用:
Try this, it worked for me:
我现在无法测试。但为什么不呢...
步骤 1. 访问 HttpServletResponse。为此,请在您的服务中声明如下内容:
步骤 2. 进行重定向
编辑
那么,要调用转发方法,您需要访问 ServletContext。它可以用与响应相同的方式解决:
现在
_context.forward
可用I can't test it right now. But why not...
Step 1. Get access to HttpServletResponse. To do it declare in your service something like:
Step 2. make redirect
EDIT
Well, to call forward method you need get to ServletContext. It is can be resolved the same way as response:
Now
_context.forward
is available