I have a scenario to create a rest method which return json response if there is any validation fail. and if everything is correct then download a pdf method.
Is it possible to be done via same method?
PFB the method signature what i need to achieve.
@RequestMapping(value = "/getPdf", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = {"application/pdf","application/json"})
public @ResponseBody LinkedHashMap<String, Object> getPdf(@Valid @RequestBody DownloadPdfDTO downloadPdfDTO,BindingResult result,HttpServletRequest request,HttpServletResponse response) throws IOException {
}
This Method works fine when everything correct and PDF is getting download.
But when there is any validation fail then no response is getting render and i am getting Status : 406 Not Acceptable .
Thanks
Yes u can dot it using global exception handler.
@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
public class GlobalExceptionHandler {
ObjectMapper objectMapper = new ObjectMapper();
@ExceptionHandler(AccessDeniedException.class)
public void handleAccessDenied(HttpServletResponse response, AccessDeniedException ex) {
response.setStatus(HttpStatus.UNAUTHORIZED);
try (PrintWriter writer = response.getWriter()) {
objectMapper.writeValue(writer, YOUR_OBJECT_TO_RETURN);
} catch (IOException ex) {
}
}
@ExceptionHandler({MethodArgumentNotValidException.class, ConstraintViolationException.class})
public void handleException(HttpServletResponse response, Exception ex) {
response.setStatus(HttpStatus.UNPROCESSABLE_ENTITY);
try (PrintWriter writer = response.getWriter()) {
objectMapper.writeValue(writer, YOUR_OBJECT_TO_RETURN);
} catch (IOException ex) {
}
}
@ExceptionHandler(NullPointerException.class)
public void handleNullPointerException(HttpServletResponse response, Exception ex) {
//return your required response
}
}
You can give a return type as ResponseEntity. With ResponseEntity you can bind different response as per your business logic.
An example will be like this
@ApiOperation(value = "Get PDF API", consumes = "application/json", httpMethod = "GET")
@ApiResponses(value = {@ApiResponse(code = 404, message = "Bad request")})
@RequestMapping(value = "/pdf/{filename}", headers = "Accept=application/json", method = RequestMethod.GET)
public
@ResponseBody
ResponseEntity getPDF(@ApiParam(value = "filename", required = true) @PathVariable String filename) {
try {
Boolean validationPass = checkValidation();
if (validationPass) {
resp = getPDF();
responseHeaders.setContentType(MediaType.APPLICATION_PDF);
responseHeaders.set("charset", "utf-8");
responseHeaders.setContentLength(resp.length);
responseHeaders.set("Content-disposition", "attachment; filename=response.pdf");
return new ResponseEntity<>(resp,responseHeaders, HttpStatus.OK);
}
else {
// create your json validation eneity here
return new ResponseEntity<>(validation entity, HttpStatus.BAD_REQUEST);
}
} catch (Exception e) {
return new ResponseEntity<>( createExceptionEntity(e) ,HttpStatus.BAD_REQUEST);
}
}
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.