泽西URL转发

Don*_*rem 7 java url forwarding jersey

在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 "";
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

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);
Run Code Online (Sandbox Code Playgroud)

小智 8

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

我在我的应用程序中使用上面的代码,它的工作原

  • 这是重定向而不是前进. (2认同)

Dew*_*wfy 2

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

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

@Context
HttpServletResponse _currentResponse;
Run Code Online (Sandbox Code Playgroud)

步骤2.进行重定向

...
_currentResponse.sendRedirect(redirect2Url);
Run Code Online (Sandbox Code Playgroud)

编辑

好吧,要调用前向方法,您需要访问 ServletContext。可以用与响应相同的方式解决:

@javax.ws.rs.core.Context 
ServletContext _context;
Run Code Online (Sandbox Code Playgroud)

现在_context.forward可用