I've been tasked to create a web service that fetches a file from an Azure storage account.
On sucess : return the file as the payload
On error : return an xml response.
The xml response will contain a copy of the request, error codes and messages etc as the user will need a helpful error message explaining what happened.
I can find instructions for how to return a single object/media type, but not multiple types dependent on condition.
Is this possible?
Solved with an exception handler thanks to Abhijeet's comment - important to add the contentType or it returns JSON by default
@ControllerAdvice
public class MyExceptionHandler {
@ResponseStatus(HttpStatus.I_AM_A_TEAPOT)
@ExceptionHandler(MyException.class)
@ResponseBody
ResponseEntity<?> exceptionHandler(MyException e){
InvoiceArchiveResponse responseObject = e.getResponseObject();
return ResponseEntity.badRequest()
.contentType(MediaType.parseMediaType("application/xml"))
.body(responseObject);
}
}
Thanks!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.