use of com.poc.restfulpoc.config.ApiError in project POC by rajadilipkolli.
the class CustomerController method createCustomer.
/**
* Create a new customer and return in response with HTTP 201
*
* @param customer a {@link com.poc.restfulpoc.entities.Customer} object.
* @return created customer
* @param ucBuilder a {@link org.springframework.web.util.UriComponentsBuilder} object.
* @param errors a {@link org.springframework.validation.Errors} object.
*/
@PostMapping(value = { "/customers/" })
public ResponseEntity<Object> createCustomer(@Valid @RequestBody Customer customer, UriComponentsBuilder ucBuilder, Errors errors) {
customerValidator.validate(customer, errors);
if (errors.hasErrors()) {
final String errorMessage = errors.getAllErrors().stream().map(ObjectError::getDefaultMessage).collect(Collectors.joining(","));
final ApiError apiError = new ApiError(HttpStatus.UNPROCESSABLE_ENTITY, new Throwable(errorMessage));
log.error("Detailed Error while processing request :{}", apiError.toString());
return new ResponseEntity<>(apiError, apiError.getStatus());
}
log.info("Creating Customer :{} ", customer.getFirstName());
if (customerService.isCustomerExist(customer)) {
log.error("A Customer with name {} already exist ", customer.getFirstName());
return new ResponseEntity<>(HttpStatus.CONFLICT);
}
customerService.createCustomer(customer);
final HttpHeaders headers = new HttpHeaders();
headers.setLocation(ucBuilder.path("/rest/customers/{customerId}").buildAndExpand(customer.getId()).toUri());
return new ResponseEntity<>(headers, HttpStatus.CREATED);
}
use of com.poc.restfulpoc.config.ApiError in project POC by rajadilipkolli.
the class RestExceptionHandler method handleEntityNotFound.
/**
* Handles EntityNotFoundException. Created to encapsulate errors with more detail
* than javax.persistence.EntityNotFoundException.
*
* @param ex the EntityNotFoundException
* @return the ApiError object
*/
@ExceptionHandler(EntityNotFoundException.class)
protected ResponseEntity<Object> handleEntityNotFound(EntityNotFoundException ex) {
final ApiError apiError = new ApiError(NOT_FOUND);
apiError.setMessage(ex.getMessage());
return buildResponseEntity(apiError);
}
use of com.poc.restfulpoc.config.ApiError in project POC by rajadilipkolli.
the class RestExceptionHandler method handleHttpMediaTypeNotSupported.
/**
* {@inheritDoc}
*
* Handle HttpMediaTypeNotSupportedException. This one triggers when JSON is invalid
* as well.
*/
@Override
protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
final StringBuilder builder = new StringBuilder();
builder.append(ex.getContentType());
builder.append(" media type is not supported. Supported media types are ");
ex.getSupportedMediaTypes().forEach(t -> builder.append(t).append(", "));
return buildResponseEntity(new ApiError(HttpStatus.UNSUPPORTED_MEDIA_TYPE, builder.substring(0, builder.length() - 2), ex));
}
use of com.poc.restfulpoc.config.ApiError in project POC by rajadilipkolli.
the class RestExceptionHandler method handleMethodArgumentNotValid.
/**
* {@inheritDoc}
*
* Handle MethodArgumentNotValidException. Triggered when an object fails @Valid
* validation.
*/
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
final ApiError apiError = new ApiError(BAD_REQUEST);
apiError.setMessage("Validation error");
apiError.addValidationErrors(ex.getBindingResult().getFieldErrors());
apiError.addValidationError(ex.getBindingResult().getGlobalErrors());
return buildResponseEntity(apiError);
}
use of com.poc.restfulpoc.config.ApiError in project POC by rajadilipkolli.
the class RestExceptionHandler method handleConstraintViolation.
/**
* Handles javax.validation.ConstraintViolationException. Thrown when @Validated
* fails.
*
* @param ex the ConstraintViolationException
* @return the ApiError object
*/
@ExceptionHandler(javax.validation.ConstraintViolationException.class)
protected ResponseEntity<Object> handleConstraintViolation(javax.validation.ConstraintViolationException ex) {
final ApiError apiError = new ApiError(BAD_REQUEST);
apiError.setMessage("Validation error");
apiError.addValidationErrors(ex.getConstraintViolations());
return buildResponseEntity(apiError);
}
Aggregations