怎么让java服务端返回401_当HTTP请求的返回状态为401时,如何解析Java中的响应正文...

我正在使用Spring RestTemplate和Jackson 的RESTful JSON API 。在某些情况下,我们可能会收到Status

401带有自定义JSON主体的(未经授权)响应,该响应由API制造商定义,如下所示:

{

"code": 123,

"message": "Reason for the error"

}

我们需要解析主体,并code在业务逻辑中使用该属性。

这是我们需要解析为的错误响应Java对象:

public class CustomError {

@JsonProperty

private Integer code;

@JsonProperty

private String message;

public Integer getCode() {

return code;

}

public String getMessage() {

return message;

}

}

还有一个自定义错误处理程序可以做到这一点:

public class CustomErrorHandler extends DefaultResponseErrorHandler {

private RestTemplate restTemplate;

private ObjectMapper objectMapper;

private MappingJacksonHttpMessageConverter messageConverter;

@Override

public boolean hasError(ClientHttpResponse response) throws IOException {

return super.hasError(response);

}

@Override

public void handleError(final ClientHttpResponse response) throws IOException {

try {

CustomError error =

(CustomError) messageConverter.read(CustomError.class, response);

throw new CustomErrorIOException(error, error.getMessage());

} catch (Exception e) {

// parsing failed, resort to default behavior

super.handleError(response);

}

}

}

错误处理程序失败,并HttpMessageNotReadableException在try块中显示:

“无法读取JSON:由于服务器身份验证,在流模式下无法重试”

这就是我发送请求的方式:

restTemplate.postForObject(url, pojoInstance, responseClass);

如果使用普通的旧客户端客户端程序(如Postman)执行相同的请求,则将收到预期的JSON响应。因此,我认为问题可能出在Spring的ClientHttpResponse实现中,在401状态下,它不允许访问响应主体。

是否确实可以解析响应主体?

更新资料

根据我的调查,RestTemplate该类使用ClientHttpResponse,进而创建sun.net.www.protocol.http.HttpURLConnection提供输入流的。在这里,输入流被忽略,并IOException抛出:

在流模式下,由于服务器身份验证而无法重试

因此,HttpURLConnection的实现导致了此问题。

可以避免这个问题吗?也许我们应该使用一种替代实现,在出现错误状态代码的情况下,它不忽略响应主体?您能推荐任何其他选择吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java后端使用HttpClient库可以发送POST请求,并提交文件流。以下是示例代码: ```java import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.InputStreamEntity; import org.apache.http.impl.client.HttpClients; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class HttpClientExample { public static void main(String[] args) throws IOException { HttpClient httpClient = HttpClients.createDefault(); // 创建POST请求 HttpPost httpPost = new HttpPost("http://example.com/upload"); // 读取文件流 File file = new File("path/to/file"); InputStream inputStream = new FileInputStream(file); // 设置请求体为文件流 InputStreamEntity entity = new InputStreamEntity(inputStream); httpPost.setEntity(entity); // 发送请求并获取响应 HttpResponse response = httpClient.execute(httpPost); // 处理响应 HttpEntity responseEntity = response.getEntity(); if (responseEntity != null) { // 处理响应内容 InputStream responseStream = responseEntity.getContent(); // ... } } } ``` 以上代码使用Apache HttpClient库发送POST请求,并将文件流作为请求体。在示例,我们将文件路径设置为`path/to/file`,你需要将其替换为实际文件的路径。然后,我们可以通过`httpClient.execute(httpPost)`发送请求并获取响应。最后,我们可以从响应获取响应内容进行处理。 请注意,这只是一个基本示例,你可能需要根据你的实际需求进行适当的修改。另外,你需要在项目添加Apache HttpClient的依赖。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值