Jersey客户端不遵循重定向(Jersey client doesn't follow r

2019-06-26 00:20发布

我有“用户”资源定义如下:

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

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response addUser(User userInfo) throws Exception {
                String userId;
        User existing = ... // Look for existing user by mail
        if (existing != null) {
            userId = existing.id;
        } else {
            userId = ... // create user
        }
        // Redirect to the user page:
        URI uri = URI.create("/api/users/" + userId);
        ResponseBuilder builder = existing == null ? Response.created(uri) : Response.seeOther(uri);
        return builder.build();
    }

    @Path("{id}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public User getUserById(@PathParam("id") String id) {
        return ... // Find and return the user object
    }
}

于是,我尝试使用Jersey客户端测试用户创建:

ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
clientConfig.getFeatures().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, Boolean.TRUE);
Client client = Client.create(clientConfig);
client.addFilter(new LoggingFilter(System.out));

User userInfo = UserInfo();
userInfo.email = "test";
userInfo.password = "test";
client.resource("http://localhost:8080/api/users")
    .accept(MediaType.APPLICATION_JSON)
    .type(MediaType.APPLICATION_JSON)
    .post(User.class, userInfo);

我得到以下异常:

重度:消息正文阅读器的Java类com.colabo.model.User和Java类型的类com.colabo.model.User和MIME媒体类型text / html; 字符集= ISO-8859-1未找到

这是HTTP请求的跟踪:

1 * Client out-bound request
1 > POST http://localhost:8080/api/users
1 > Accept: application/json
1 > Content-Type: application/json
{"id":null,"email":"test","password":"test"}
1 * Client in-bound response
1 < 201
1 < Date: Tue, 03 Jul 2012 06:12:38 GMT
1 < Content-Length: 0
1 < Location: /api/users/4ff28d5666d75365de4515af
1 < Content-Type: text/html; charset=iso-8859-1
1 <

应该Jersey客户端按照这种情况下自动重定向,妥善解组,并从第二个请求返回一个JSON对象?

谢谢,迈克尔

Answer 1:

执行重定向手段以下30X状态代码重定向。 你有没有什么是201的响应,这不是一个重定向。 你可以写一个ClientFilter ,将按照位置标头如果返回201。



Answer 2:

我面临着同样的问题,并通过客户机滤解决它

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());
    }
}

现在,而在其他类要在其中创建客户端的

Client client = ClientBuilder.newClient();

应用此过滤器类为

client.register(RedirectFilterWorkAround.class);

这正与发现的SO这些指针新泽西2.X:P



文章来源: Jersey client doesn't follow redirects