我想我的应用程序出错了。
当我试图从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 1ff8\r\n{"data":...1ff8\r\nSim"[9867,"CAR\r\n1c7d\r\n...}\r\n0\r\n\r\n当我不使用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.:“.”部分只是我没有在这里传递所有的数据。
有人也有同样的问题吗?似乎有些字符有不同的编码类型。
编辑1:
如果我试图直接访问apiman url,响应将显示为ok:
https://apiman.url/apiman-gateway/org/api/1.0/method?apikey=894fd5b3-36f0-32974-xxxxxxxx
// response is ok {"data":...Sim"[9867,"CAR...}在另一个测试中,我使用了cUrl方法:
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
*/编辑2:
由于某种原因,问题再次发生。
有办法解决这个问题吗?
发布于 2017-01-03 16:39:19
一种解决方案是创建一个自定义策略插件并覆盖doApply方法,以删除使用ApiResponse对象的Transfer-Encoding标头(如果它等于chunked )。
apiresponse.getHeaders().remove("Transfer-Encoding");并将新的自定义策略插件添加到服务中。这是一个简单的教程来创建插件
https://stackoverflow.com/questions/41385736
复制相似问题