泽西URL转发

时间:2011-11-23 13:09:29

标签: java url jersey forwarding

在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 scope for proxy of class $Proxy78错误:

@Context
HttpServletRequest request;
@Context
HttpServletResponse response;
@Context
ServletContext context;

...

RequestDispatcher dispatcher =  context.getRequestDispatcher("url");
dispatcher.forward(request, response);

3 个答案:

答案 0 :(得分:8)

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

我在我的应用程序中使用了上面的代码并且它可以工作。

答案 1 :(得分:3)

试试这个,它对我有用:

public String getForwardNews(

@Context final HttpServletRequest request,

@Context final HttpServletResponse response) throws Exception

{

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

response.sendRedirect("YourUrlSite");

return "";

}

答案 2 :(得分:2)

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

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

@Context
HttpServletResponse _currentResponse;

步骤2.进行重定向

...
_currentResponse.sendRedirect(redirect2Url);

修改

好吧,要调用forward方法,你需要访问ServletContext。它可以像响应一样解决:

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

现在_context.forward可用