gpt4 book ai didi

java - 无法从 Spring boot 中的 REST API 返回错误代码和消息

转载 作者:行者123 更新时间:2023-12-02 09:46:45 24 4
gpt4 key购买 nike

我是 Spring Boot 新手。我在Spring boot中实现了以下rest api:

 @GetMapping(value = "/output")
public ResponseEntity<?> getListOfPOWithItem(@QueryParam("input1") String input1,
@QueryParam("input2") String input2)
throws BusinessException {
if (input1 == null) {
throw new BusinessException("Query param input1 is null or invalid");
}
if (input2 == null) {
throw new BusinessException("Query param input2 is null or invalid");
}


List<Output> outputList =

myService.getDetails(input1, input2);

if (outputList != null) {
return new ResponseEntity<List<Ouput>>(outputList, HttpStatus.OK);
}
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}

Myservice 中的 getDetails() 定义如下:

public List<Output> getDetails(String input1, String input2)
throws BusinessException {
String path = new StringBuilder().append(getBaseUrl()).append("/input1/")
.append(input1).append("/input2/").append(input2).toString();
try {
ResponseEntity<List<Output>> responseEntityList = restTemplate.exchange(path,
HttpMethod.GET, null, new ParameterizedTypeReference<List<Output>>() {});
List<Output> outputList = responseEntity.getBody();

if (responseEntityList.isEmpty()) {
throw new EntityNotFoundException("Input not found",
ExternalServicesErrorCode.NO_DATA_FOUND);
}
return outputList;

} catch (HttpStatusCodeException e) {
int statusCode = e.getStatusCode().value();

if (statusCode == Status.NOT_FOUND.getStatusCode()) {
throw new EntityNotFoundException("Data not found",
ExternalServicesErrorCode.NO_DATA_FOUND);

} else {
throw new BusinessException("Error in getting data", ExternalServicesErrorCode.SERVICE_ERROR);
}
}

}

问题是:在针对无效输入调用此 API 时,我收到 500,而不是 404 和错误消息“未找到数据”。谁能建议我应该对上面的代码进行哪些更改?

编辑:按照建议,我添加了以下类(class):

@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

@ExceptionHandler(Exception.class)
public final ResponseEntity<ExceptionResponse>
handleAllExceptions(Exception ex,
WebRequest request) {

ExceptionResponse exceptionResponse = new ExceptionResponse(Instant.now().toEpochMilli(),
ex.getMessage(), request.getDescription(true));
return new ResponseEntity<>(exceptionResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(EntityNotFoundException.class)
public final ResponseEntity<ExceptionResponse> handleEntityNotFoundException(
EntityNotFoundException ex, WebRequest request) {

ExceptionResponse exceptionResponse = new ExceptionResponse(Instant.now().toEpochMilli(),
ex.getMessage(), request.getDescription(true));
return new ResponseEntity<>(exceptionResponse, HttpStatus.NO_CONTENT);
}

即使在那之后我也无法按预期获得错误代码和错误消息。

最佳答案

如果两个输入都是无效的错误消息,您应该创建一条组合消息,而不是抛出业务异常,然后您可以返回一个responseEntity作为快速修复:
返回新的ResponseEntity<>("无效输入", HttpStatus.NOT_FOUND);

关于java - 无法从 Spring boot 中的 REST API 返回错误代码和消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56591105/

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