问题
I am new to Spring boot. I have implemented following rest api in Spring boot:
@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();
}
getDetails() in Myservice is defined as below:
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);
}
}
}
Problem is: while invoking this API for invalid inputs, I am getting 500, not 404 and error message "No data found". Can anyone please suggest what change should I make in the above code ?
EDIT: As suggested, I have added following 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);
}
Even after that I am unable to get the error code and error message as expected.
回答1:
As per your controller it uses to throw "BusinessException" exception. But you have not implemented a method to catch that exception in controller adviser which is "GlobalExceptionHandler ". Please include below method in your controller adviser as test was successful.
@ExceptionHandler(BusinessException.class)
public ResponseEntity<String> handleBusinessException(BusinessException businessException ) {
return new ResponseEntity<>("Your specific error", HttpStatus.NOT_FOUND);
}
Below is the test outcome
回答2:
Exception Handling code is missing in your case, refer to https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.html and https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/ExceptionHandler.html
回答3:
Instead of throwing business exception you should create a combined message if both inputs are invalid error message and then you can return a responseEntity as a quick fix:
return new ResponseEntity<>("Invalid input", HttpStatus.NOT_FOUND);
回答4:
To handle this fix through @RestControllerAdvice , you should create a custom exception which contains the httpStatus Code and the message which you want to return
public class CustomBusinessException extends Exception{
/**
*
*/
private static final long serialVersionUID = 1L;
private final String status;
private final String requestMessage;
private final String description;
public CustomBusinessException (Throwable ex,String status, String requestMessage, String description) {
super(ex);
this.status = status;
this.requestMessage = requestMessage;
this.description = description;
}
//create getter and setter
}
handle this exception through your custom exception handler(@RestCOntrollerAdvice) and prepare a custom response to be sent like this
@ExceptionHandler({ CustomBusinessException .class })
public ResponseEntity<CustomBusinessResponse> handleAll(CustomBusinessException customBusinessException , WebRequest request) {
logger.error("Exception occured in NRACustomExceptionHandler :"+ExceptionUtils.getStackTrace(nraGatewayException));
CustomBusinessResponse response = new CustomBusinessResponse();
response.setMessage(customBusinessException.getRequestMessage());
response.setDescription(customBusinessException.getDescription());
return new ResponseEntity<>(response, new HttpHeaders(), HttpStatus.valueOf(Integer.parseInt(customBusinessException.getStatus())));
}
Create a custom response class
public class NRAExceptionResponse {
private String message;
private String description;
//create getter and setter
}
throw the custom exception with status to be sent like this
throw new NRAGatewayException(e, "404","Invalid Input", "Invalid input 1 and input 2");
来源:https://stackoverflow.com/questions/56591105/not-able-to-return-error-code-with-message-from-rest-api-in-spring-boot