Search in sources :

Example 1 with ApiError

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) ApiError(com.poc.restfulpoc.config.ApiError) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 2 with ApiError

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);
}
Also used : ApiError(com.poc.restfulpoc.config.ApiError) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 3 with 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));
}
Also used : ApiError(com.poc.restfulpoc.config.ApiError)

Example 4 with ApiError

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);
}
Also used : ApiError(com.poc.restfulpoc.config.ApiError)

Example 5 with 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);
}
Also used : ApiError(com.poc.restfulpoc.config.ApiError) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Aggregations

ApiError (com.poc.restfulpoc.config.ApiError)7 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)3 ResponseEntityExceptionHandler (org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)3 HttpHeaders (org.springframework.http.HttpHeaders)1 ResponseEntity (org.springframework.http.ResponseEntity)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)1