JAX-RS客户端调用通过应用程序返回格式错误的语法,但在POSTMAN应用程序中可以正常工作

时间:2019-01-30 06:35:27

标签: jax-rs jersey-client

我们需要从我们的应用中调用第三方API。该调用在大多数情况下都可以正常运行,但有时我们会从第三方服务器收到错误消息,说

{
     "type":"https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1",
     "title":"The request could not be understood by the server due to malformed syntax"
}

POSTMAN发出的相同请求效果很好。我已根据请求和响应为记录器附加了过滤器。下面是记录器的请求响应。

[5e311624-15b8-47d5-87ea-15269132d576] [myproject] 2019-01-28 11:49:39,621 [http-nio-8080-exec-14] INFO  mypackage.client.filter.EntityLoggingFilter - {
  "id" : "293",
  "value" : "My Test Course 1, My Test Course 2, My Test Course 3, My Test Course 4, My Test Course 5, My Test Course 6, My Test Course 7, My Test Course 8, My Test Course 9, My Test Course 10, My Test Course 11, My Test Course 12, My Test Course 13, My Test Course 14, My Test Course 15, My Test Course 16, My Test Course 17, My Test Course 18, My Test Course 19, My Test Course 20, My Test Course 21, My Test Course 22, My Test Course 23, My Test Course 24, My Test Course 25, My Test Course 26, My Test Course 27, My Test Course 28, My Test Course 29, My Test Course 30, My Test Course 31, My Test Course 32, My Test Course 33, My Test Course 34, My Test Course 35, My Test Course 36, My Test Course 37, My Test Course 38, My Test Course 39, My Test Course 40, My Test Course 41, My Test Course 42, My Test Course 43, My Test Course 44, My Test Course 45, My Test Course 46, My Test Course 47, My Test Course 48, My Test Course 49, My Test Course 50, My Test Course 51, My Test Course 52, My Test Course 53, My Test Course 54, My Test Course 55, My Test Course 56, My Test Course 57, My Test Course 58, My Test Course 59, My Test Course 60, My Test Course 61, My Test Course 62, My Test Course 63, My Test Course 64, My Test Course 65, My Test Course 66, My Test Course 67, My Test Course 68, My Test Course 69, My Test Course 70, My Test Course 71, My Test Course 72, My Test Course 73, My Test Course 74, My Test Course 75, My Test Course 76, My Test Course 77, My Test Course 78, My Test Course 79, My Test Course 80, My Test Course 81, My Test Course 82, My Test Course 83, My Test Course 84, My Test Course 85, My Test Course 86, My Test Course 87, My Test Course 88, My Test Course 89, My Test Course 90, My Test Course 91, My Test Course 92, My Test Course 93, My Test Course 94, My Test Course 95, My Test Course 96, My Test Course 97, My Test Course 98, My Test Course 99, My Test Course 100, My Test Course 101, My Test Course 102, My Test Course 103, My Test Course 104, My Test Course 105, My Test Course 106, My Test Course 107, My Test Course 108, My Test Course 109, My Test Course 110, My Test Course 111, My Test Course 112, My Test Course 113, My Test Course 114, My Test Course 115, My Test Course 116, My Test Course 117, My Test Course 118, My Test Course 119, My Test Course 120, My Test Course 121, My Test Course 122, My Test Course 123, My Test Course 124, My Test Course 125, My Test Course 126, My Test Course 127, My Test Course 128, My Test Course 129, My Test Course 130, My Test Course 131, My Test Course 132, My Test Course 133, My Test Course 134, My Test Course 135, My Test Course 136, My Test Course 137, My Test Course 138, My Test Course 139, My Test Course 140, My Test Course 141, My Test Course 142, My Test Course 143, My Test Course 144, My Test Course 145, My Test Course 146, My Test Course 147, My Test Course 148, My Test Course 149, My Test Course 150, My Test Course 151, My Test Course 152, My Test Course 153, My Test Course 154, My Test Course 155, My Test Course 156, My Test Course 157, My Test Course 158, My Test Course 159, My Test Course 160, My Test Course 161, My Test Course 162, My Test Course 163, My Test Course 164, My Test Course 165, My Test Course 166, My Test Course 167, My Test Course 168, My Test Course 169, My Test Course 170, My Test Course 171, My Test Course 172, My Test Course 173, My Test Course 174, My Test Course 175, My Test Course 176, My Test Course 177, My Test Course 178, My Test Course 179, My Test Course 180, My Test Course 181, My Test Course 182, My Test Course 183, My Test Course 184, My Test Course 185, My Test Course 186, My Test Course 187, My Test Course 188, My Test Course 189, My Test Course 190, My Test Course 191, My Test Course 192, My Test Course 193, My Test Course 194, My Test Course 195, My Test Course 196, My Test Course 197, My Test Course 198, My Test Course 199, My Test Course 200, My Test Course 201, My Test Course 202, My Test Course 203, My Test Course 204, My Test Course 205, My Test Course 206, My Test Course 207, My Test Course 208, My Test Course 209, My Test Course 210"
}

[5e311624-15b8-47d5-87ea-15269132d576] [myproject] 2019-01-28 11:49:39,716 [http-nio-8080-exec-14] INFO  mypackage.client.filter.EntityLoggingFilter - {"type":"https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1","title":"The request could not be understood by the server due to malformed syntax"}

上述请求在我的代码中始终失败,但是同一请求在POSTMAN中效果很好(从记录器复制到邮递员相同的上述请求)。 MyDTO还具有字段和getter / setter。

代码:

public MyDTO create(String path, MyDTO entity) {
    Invocation invocation = getTarget()
            .path(path)
            .request(MediaType.APPLICATION_JSON)
            .buildPost(Entity.entity(entity, MediaType.APPLICATION_JSON));
    Response response = invocation.invoke();
    validateResponse(response, Status.CREATED);
    return response.readEntity(new GenericType<MyDTO>() {});
}

private WebTarget getTarget() {
    Client client = client = new OpenSSLClientBuilder()
            .authentication(username, password)
            .proxy(proxy)
            .buildClient();        
    client.register(new EntityLoggingFilter());
    return client.target(UriBuilder.fromUri(getProperty(BASEURI)));
}

MYDTO

@JsonInclude(Include.NON_NULL)
public class MyDTO implements Serializable
{
    private String id;
    private String value;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "MyDTO{" + "id=" + id + ", value=" + value + '}';
    }   
}

错误响应:

{
    "type":"https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1",
    "title":"The request could not be understood by the server due to malformed syntax"
}

0 个答案:

没有答案