泽西岛客户端不遵循重定向

时间:2012-07-03 06:25:18

标签: rest jersey jax-rs

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

@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);

我得到以下异常:

  

SEVERE:Java类com.colabo.model.User的消息体阅读器,   和Java类型类com.colabo.model.User和MIME媒体类型   为text / html; charset = 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对象?

谢谢, 迈克尔

2 个答案:

答案 0 :(得分:1)

关注重定向意味着跟随30x状态代码重定向。您所拥有的是201响应,不是重定向。如果返回201,您可以编写一个ClientFilter,它将跟随位置标题。

答案 1 :(得分:1)

我遇到了同样的问题,并通过关注客户端过滤器

解决了这个问题
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);

这适用于Jersey 2.x 在SO:p

上找到了这些指针
相关问题