Jersey client doesn't follow redirects

后端 未结 2 863
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-07 14:03

I have \"users\" resource defined as follows:

@Path(\"/api/users\")
public class UserResource {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    publ         


        
相关标签:
2条回答
  • 2021-01-07 14:31

    Follow redirects means following 30x status code redirects. What you have there is a 201 response, that is not a redirect. You can write a ClientFilter that will follow the location header if 201 is returned.

    0 讨论(0)
  • 2021-01-07 14:38

    I was facing the same issue and resolved it by following client filter

    package YourPackageName;
    
    
    import javax.ws.rs.client.ClientRequestContext;
    import javax.ws.rs.client.ClientResponseContext;
    import javax.ws.rs.client.ClientResponseFilter;
    import javax.ws.rs.core.Response;
    import java.io.IOException;
    import java.io.InputStream;
    
    public class RedirectFilterWorkAround implements ClientResponseFilter {
        @Override
        public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
            if (responseContext.getStatusInfo().getFamily() != Response.Status.Family.REDIRECTION)
                return;
    
            Response resp = requestContext.getClient().target(responseContext.getLocation()).request().method(requestContext.getMethod());
    
            responseContext.setEntityStream((InputStream) resp.getEntity());
            responseContext.setStatusInfo(resp.getStatusInfo());
            responseContext.setStatus(resp.getStatus());
        }
    }
    

    Now while in other class where you are creating Client as

    Client client = ClientBuilder.newClient();
    

    Apply this filter class as

    client.register(RedirectFilterWorkAround.class);
    

    This is working with Jersey 2.x Found these pointers on SO :p

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