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

2023-09-06 15:14:01 作者:坠欢

I have "users" resource defined as follows:

@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
    }
}

Then, I'm trying to test user creation using Jersey client:

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);
驻西使馆再次调整检测机构范围,ECHEVARNE化验室全部上榜 开通中文客服方便侨胞预约

I get the following exception:

SEVERE: A message body reader for Java class com.colabo.model.User, and Java type class com.colabo.model.User, and MIME media type text/html; charset=iso-8859-1 was not found

And this is the trace of the HTTP request:

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 <

Should Jersey client follow redirect automatically in this case, and properly unmarshall and return a Json object from the second request?

Thanks, Michael

解决方案

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