Spring ResponseEntity return custom Status Code

Viewed 1935

I'm trying to implement a custom HttpStatus code system to give errors informations on my REST services.

To do so, i've implemented a new Enum, CustomHttpStatus

public enum CustomHttpStatus {

    ERROR1(42, "Error 1"),
    ERROR2(43, "Error 2"),
    ERROR3(44, "Error 3"),
    ERROR4(45, "Error 4"),
    ERROR5(46, "Error5");

    private CustomHttpStatus(int value, String reason) {
        this.value = value;
        this.reason = reason;
    }

    private final int value;

    private final String reason;

    public int getValue() {
        return value;
    }

    public String getReason() {
        return reason;
    }

}

Then, i have a class which carries the information about the error and manages standard HTTPStatus and my CustomHttpStatus

public class ApiError {

    private HttpStatus status;
    private CustomHttpStatus customStatus;
    private String message;
    private List<String> errors;

    public HttpStatus getStatus() {
        return status;
    }

    public void setStatus(HttpStatus status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<String> getErrors() {
        return errors;
    }

    public void setErrors(List<String> errors) {
        this.errors = errors;
    }



    public ApiError(HttpStatus status, String message, List<String> errors) {
        super();
        this.status = status;
        this.message = message;
        this.errors = errors;

    }

    public ApiError(HttpStatus status, String message, String error) {
        super();
        this.status = status;
        this.message = message;
        errors = Arrays.asList(error);

    }

    public ApiError(CustomHttpStatus customStatus, String message, List<String> errors) {
        super();
        this.customStatus = customStatus;
        this.message = message;
        this.errors = errors;

    }

    public ApiError(CustomHttpStatus customStatus, String message, String error) {
        super();
        this.customStatus = customStatus;
        this.message = message;
        errors = Arrays.asList(error);

    }




    public CustomHttpStatus getCustomStatus() {
        return customStatus;
    }

    public void setCustomStatus(CustomHttpStatus customStatus) {
        this.customStatus = customStatus;
    }

}

Next, in a @ControllerAdvice class i define how to respond to errors, for instance

    @ExceptionHandler(PlateNumberExistsExceptionAPI.class )
        protected ResponseEntity<Object> handlePlateNumberExistExceptionAPI(PlateNumberExistsExceptionAPI ex, WebRequest request) {

    StringBuilder builder = new StringBuilder();
   builder.append(ex.getMessage());
    ApiError apiError = new ApiError(CustomHttpStatus.ERROR1, 
    ex.getLocalizedMessage(), builder.substring(0,      builder.length()));
    logger.debug("ApiError is " + apiError.toString());
    logger.error("Plate exists - API", ex);
    return ResponseEntity.status(apiError.getCustomStatus().getValue())
.headers(new HttpHeaders())    
.contentType(MediaType.APPLICATION_JSON).body(apiError);


         }

The problem is that i need to use a BodyBuilder for ResponseEntity which constructor want a HttpStatus value. The application responds with my custom error, but with no message and it takes 60000 ms to answer... I don't know why it takes so long, the exception is soon hit and the ApiError generated, the problem is on the response

PostMan Test

0 Answers
Related