gpt4 book ai didi

java - 如何使用错误响应休息模板处理 200 状态代码?

转载 作者:行者123 更新时间:2023-12-01 17:10:14 28 4
gpt4 key购买 nike

我想处理 200 错误响应

final ResponseEntity<ResponseType> responseEntity = restTemplate.postForEntity(url, requestEntity, ResponseType.class);

这里我直接在休息模板中使用响应模型对象,下面是我的catch block

catch (final RestClientException | HttpMessageConversionException ex) {
throw new CustomException(message, ex);
}

这里我添加了HttpMessageConversionException因为我收到 200 错误响应,并且在将其转换为其余模板中的成功响应类型类时,它会抛出错误,因为在我的成功响应类构造函数中具有字段验证。

下面是我在休息模板中使用的响应类

@JsonDeserialize(builder = ResponseType.Builder.class)
@JacksonXmlRootElement(localName = "Test")
public class ResponseType {

private final String details;

private ResponseType(final Builder builder) {
this.details = OptionalCheck.checkPresent(builder.details, "details");
}

public static Builder builder() {
return new Builder();
}

//getter

public static final class Builder {
private Optional<String> details = Optional.empty();

@JacksonXmlProperty(localName = "details")
public Builder withDetails(final String theDetails) {
this.details = Optional.ofNullable(theDetails);
return this;
}


public ResponseType build() {
return new ResponseType(this);
}
}
}

下面是堆栈跟踪

org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.test.ResponseType$Builder]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.test.ResponseType$Builder`, problem: details must be present.
at [Source: (ByteArrayInputStream); line: 5, column: 1]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:242)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:227)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:102)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:994)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:977)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:737)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670)
at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:445)

    Caused by: java.lang.IllegalArgumentException: details must be present.
at com.test.OptionalCheck.lambda$checkPresent$0(OptionalCheck.java:46)
at java.base/java.util.Optional.orElseThrow(Optional.java:408)

还有其他方法可以解决这个问题吗?

最佳答案

为了获得更好的帮助,您应该向我们展示良好响应和错误响应的示例。

但我将通过一个示例来说明您应该如何执行此操作。

假设一个好的响应如下所示:

{
"details": {
...
}
}

错误响应如下所示:

{
"error": {
...
}
}

然后,您必须将响应类型定义为具有 2 个字段的类:detailserror,然后检查哪个字段存在并根据需要使用react,例如

DetailsOrErrorResponse response = restTemplate.getForObject(url, DetailsOrErrorResponse.class);
if (response.getError() != null) {
// handle error here
} else if (response.getDetails() == null) {
throw new IllegalArgumentException("Invalid response: Missing `details` or `error`");
} else {
// handle good response here
}

关于java - 如何使用错误响应休息模板处理 200 状态代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61434958/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com