泽西岛 URL 转发

发布于 2024-12-17 18:32:06 字数 802 浏览 3 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黑寡妇 2024-12-24 18:32:06
public Response foo()
{
    URI uri = UriBuilder.fromUri(<ur url> ).build();
    return Response.seeOther( uri ).build();
}

我在我的应用程序中使用了上面的代码并且它有效。

public Response foo()
{
    URI uri = UriBuilder.fromUri(<ur url> ).build();
    return Response.seeOther( uri ).build();
}

I used above code in my application and it works.

无需解释 2024-12-24 18:32:06

试试这个,它对我有用:

public String getForwardNews(

@Context final HttpServletRequest request,

@Context final HttpServletResponse response) throws Exception

{

System.out.println("CMSredirecting... ");

response.sendRedirect("YourUrlSite");

return "";

}

Try this, it worked for me:

public String getForwardNews(

@Context final HttpServletRequest request,

@Context final HttpServletResponse response) throws Exception

{

System.out.println("CMSredirecting... ");

response.sendRedirect("YourUrlSite");

return "";

}
不美如何 2024-12-24 18:32:06

我现在无法测试。但为什么不呢...

步骤 1. 访问 HttpServletResponse。为此,请在您的服务中声明如下内容:

@Context
HttpServletResponse _currentResponse;

步骤 2. 进行重定向

...
_currentResponse.sendRedirect(redirect2Url);

编辑

那么,要调用转发方法,您需要访问 ServletContext。它可以用与响应相同的方式解决:

@javax.ws.rs.core.Context 
ServletContext _context;

现在 _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:

@Context
HttpServletResponse _currentResponse;

Step 2. make redirect

...
_currentResponse.sendRedirect(redirect2Url);

EDIT

Well, to call forward method you need get to ServletContext. It is can be resolved the same way as response:

@javax.ws.rs.core.Context 
ServletContext _context;

Now _context.forward is available

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文