【问题标题】:Reading rest service of content type application/vnd.oracle.adf.resourceitem+json读取内容类型application/vnd.oracle.adf.resourceitem+json的rest服务
【发布时间】:2017-05-02 12:39:37
【问题描述】:

我有一个内容类型为 application/vnd.oracle.adf.resourceitem+json 的 Web 服务。

点击这个服务得到的响应的HttpEntity是这样的

ResponseEntityProxy{[Content-Type: application/vnd.oracle.adf.resourceitem+json,Content-Length: 3,Chunked: false]}

当我尝试将此 HttpEntity 转换为字符串时,它给了我一个空白字符串 {}

以下是我尝试将HttpEntity 转换为String 的方法

1.

String strResponse = EntityUtils.toString(response.getEntity());

2.

String strResponse = "";
String inputLine;
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
try {
    while ((inputLine = br.readLine()) != null) {
        System.out.println(inputLine);
        strResponse += inputLine;
    }
    br.close();
} catch (IOException e) {
    e.printStackTrace();
}

3.

response.getEntity().writeTo(new FileOutputStream(new File("C:\\Users\\harshita.sethi\\Documents\\Chabot\\post.txt")));

全部返回字符串 -> {}

谁能告诉我我做错了什么?

这是因为内容类型吗?

【问题讨论】:

    标签: java web-services rest http-headers oracle-adf


    【解决方案1】:

    上面的代码仍然给出与空 JSON 对象相同的响应。所以我修改并编写了以下代码。这个似乎运行得很好。

    URL url = new URL(urlString);
    HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
    
    con.setDoOutput(true);
    con.setRequestMethod("POST");
    con.addRequestProperty("Authorization", getAuthToken());
    con.addRequestProperty("Content-Type", "application/vnd.oracle.adf.resourceitem+json;charset=utf-8");
    
    String input = String.format("{\"%s\":\"%s\",\"%s\":\"%s\"}", field, value, field2, value2);
    
    System.out.println(input);
    OutputStream outputStream = con.getOutputStream();
    outputStream.write(input.getBytes());
    outputStream.flush();
    
    con.connect();
    System.out.println(con.getResponseCode());
    // Uncompressing gzip content encoding
    GZIPInputStream gzip = new GZIPInputStream(con.getInputStream());
    
    StringBuffer szBuffer = new StringBuffer();
    
    byte tByte[] = new byte[1024];
    
    while (true) {
        int iLength = gzip.read(tByte, 0, 1024);
    
        if (iLength < 0) {
            break;
        }
    
        szBuffer.append(new String(tByte, 0, iLength));
    }
    con.disconnect();
    
    returnString = szBuffer.toString();
    

    认证方式

    private String getAuthToken() {
            String name = user;
            String pwd = this.password;
            String authString = name + ":" + pwd;
            byte[] authEncBytes = Base64.getEncoder().encode(authString.getBytes());
            System.out.println(new String(authEncBytes));
            return "Basic " + new String(authEncBytes);
        }
    

    如果有人面临同样的问题。让我分享一下我面临的挑战以及我是如何解决这些挑战的。

    以上代码适用于所有内容类型/方法。可用于任何类型(GET、POST、PUT、DELETE)。 对于我的要求,我有一个 POST 网络服务与

    内容编码→gzip

    内容类型 →application/vnd.oracle.adf.resourceitem+json

    挑战:我能够获得正确的响应代码,但我收到垃圾字符作为我的响应字符串。

    解决方案:这是因为输出被压缩成gzip格式,需要解压缩。

    上面也提到了解压gzip content encoding的代码。

    希望对未来的用户有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-21
      • 1970-01-01
      • 2020-03-20
      • 2019-08-01
      • 2016-11-22
      • 2019-12-16
      • 2012-12-16
      • 2022-12-22
      相关资源
      最近更新 更多