Jersey URL forwarding

前端 未结 3 1159
心在旅途
心在旅途 2021-02-14 21:24

Inside a Jersey REST method I would like to forward to an another website. How can I achieve that?

@Path(\"/\")
public c         


        
相关标签:
3条回答
  • 2021-02-14 21:59

    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 "";
    
    }
    
    0 讨论(0)
  • 2021-02-14 22:06
    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.

    0 讨论(0)
  • 2021-02-14 22:06

    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

    0 讨论(0)
提交回复
热议问题