当剩余API通过APIMAN时,如何避免编码块



我相信我在应用程序上有一个错误。

当我尝试从URL检索信息时,我会在JSON对象上出现错误。之所以发生,是因为某些原因,Apiman在响应中包含了一种编码。这是我的代码:

Client client = Client.create();
WebResource webResource = client.resource(uri);
ClientResponse response = webResource.header("X-API-Key", xApiKey)
    .header("Content-Type", "application/json; charset=UTF-8")
    .header("Accept", "application/json")
    .header("Authorization", "Bearer " + token)
    .get(ClientResponse.class);
String json = response.getEntity(String.class);

当我通过apiman发送请求时,我的回复会带有特殊字符:

public static void main(String[] args) {
    String uri = "https://apiman.url/apiman-gateway/org/api/1.0/method";
    String apiKey = "894fd5b3-36f0-32974-xxxxxxxx"; 
    get("token", uri, apiKey);
}
// response is 1ff8rn{"data":...1ff8rnSim"[9867,"CARrn1c7drn...}rn0rnrn

当我不使用apiman时,我的响应是不同的,如下所示:

public static void main(String[] args) {
    String uri = "https://192.168.56.22:8080/app/method";
    String apiKey = "not needed"; 
    get("token", uri, apiKey);
}
// response is ok {"data":...Sim"[9867,"CAR...}

ps.:'...'的一部分只是我不在此处传递所有数据。

有人有同样的问题吗?似乎有些字符具有不同的编码类型。

edit.1:

如果我尝试直接访问apiman URL,则响应的显示正常:

https://apiman.url/apiman-gateway/org/api/1.0/method?apikey=894fd5b3-36f0-32974-xxxxxxxx
// response is ok {"data":...Sim"[9867,"CAR...}

在另一个测试中,我使用了卷曲方法:

curl -i -X GET -H "Content-Type:application/json" -H "X-API-Key:894fd5b3-36f0-32974-xxxxxxxx" 'https://apiman.url/apiman-gateway/org/api/1.0/method'
/* response is 1ff8
{"data":...
1ff8
Sim"[9867,"CAR
1c7d
...}
0
*/

edit.2:

由于某种原因,问题再次发生。

有没有办法解决此问题?

一个解决方案是创建一个自定义策略插件和覆盖doApply方法,如果使用ApiResponse对象等于chunked,则删除Transfer-Encoding标头。

apiresponse.getHeaders().remove("Transfer-Encoding");

并将新的自定义策略插件添加到您的服务中。这是一个简单的教程,用于创建插件

最新更新