Last active
June 15, 2016 07:35
-
-
Save robinraju/d09b7fe39ff3799da55885830f553627 to your computer and use it in GitHub Desktop.
JAX-RS Exception mapper for returning http response messages from Hibernate Validator or any other bean validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mycompany.error; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import lombok.Data; | |
@Data | |
public class Error { | |
@JsonProperty(value = "errorMessage") | |
private String errorMessage; | |
public Error(String message){ | |
this.errorMessage = message; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mycompany.error; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import lombok.Data; | |
import java.util.ArrayList; | |
import java.util.List; | |
@Data | |
public class ErrorList { | |
@JsonProperty(value = "errors") | |
private List<Error> errors; | |
public ErrorList(){ | |
errors = new ArrayList<>(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.extern.slf4j.Slf4j; | |
import com.mycompany.error.Error; | |
import com.mycompany.error.ErrorList; | |
import org.apache.cxf.validation.ResponseConstraintViolationException; | |
import javax.validation.ConstraintViolation; | |
import javax.validation.ConstraintViolationException; | |
import javax.validation.ValidationException; | |
import javax.ws.rs.core.Response; | |
import javax.ws.rs.core.Response.Status; | |
import javax.ws.rs.ext.ExceptionMapper; | |
import javax.ws.rs.ext.Provider; | |
import java.util.Iterator; | |
import java.util.List; | |
@Slf4j | |
@Provider | |
public class ValidationExceptionMapper implements ExceptionMapper<ValidationException> { | |
public ValidationExceptionMapper() { | |
} | |
public Response toResponse(ValidationException exception) { | |
Status errorStatus = Status.INTERNAL_SERVER_ERROR; | |
ErrorList errorList = new ErrorList(); | |
List<Error> errors = errorList.getErrors(); | |
if (exception instanceof ConstraintViolationException) { | |
ConstraintViolationException constraint = (ConstraintViolationException) exception; | |
Iterator it = constraint.getConstraintViolations().iterator(); | |
while (it.hasNext()) { | |
ConstraintViolation violation = (ConstraintViolation) it.next(); | |
errors.add(new Error(violation.getMessage())); | |
log.warn(violation.getRootBeanClass().getSimpleName() + "." + violation.getPropertyPath() + ": " + violation.getMessage()); | |
} | |
errorList.setErrors(errors); | |
if (!(constraint instanceof ResponseConstraintViolationException)) { | |
errorStatus = Status.BAD_REQUEST; | |
} | |
} else{ | |
errors.add(new Error("Error processing request")); | |
errorList.setErrors(errors); | |
} | |
return Response.status(errorStatus).entity(errorList).build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment