I am getting a below error in browser but in postman it is working fine properly. I have tried to modify the custom error handling, but all values are getting null in both browser and postman. Previously it was in postman with the whitelabel Error Page in browser. PFB code. Can anyone help me how to get the values properly in Browser and postman. Can anyone tell me why the values are coming as null values. Appreciate any help.

enter image description here

POJO Class:

import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.Date;

public class ErrorMessage {

    private int statusCode;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
    private Date timestamp;

    private String message;
    private String path;
    private String error;

    public ErrorMessage(int value, Date date, String message, String resourceNotFound) {
    }

    public ErrorMessage(String status, String message, String stackTrace) {
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

    public String getMessage() {
        return message;
    }

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

   

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }
}

CustomExceptionClass:

public class CustomDataNotFoundException extends RuntimeException {
    public CustomDataNotFoundException() {
        super();
    }

    public CustomDataNotFoundException(String message) {
        super(message);
    }
}

Exception Handler :


@ControllerAdvice
public class ValidationExceptionHandler
         {



    @ExceptionHandler(CustomDataNotFoundException.class)
    public ResponseEntity<ErrorMessage> handleCustomDataNotFoundExceptions(
            Exception e
    ) {
        HttpStatus status = HttpStatus.NOT_FOUND; // 404

        // converting the stack trace to String
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        e.printStackTrace(printWriter);
        String stackTrace = stringWriter.toString();

        return new ResponseEntity<>(
                new ErrorMessage(
                        "404",
                        e.getMessage(),
                        stackTrace
                ),
                status
        );
    }

}

Above code is dispalying the error message like below in browser:

enter image description here

Postman:

{
    "statusCode": 0,
    "timestamp": null,
    "message": null,
    "path": null,
    "error": null
}

Controller:

if (value!= null && !value.isEmpty())
  documentMap.put("value", value
if (color != null && !color.isEmpty())
   documentMap.put("color", color);
List details= collection.aggregate(pipelines).into(aggregate);
 if (details.isEmpty())
    throw new CustomDataNotFoundException(RESOURCENOTFOUND);
    return getPrettyDocument(details);
0

There are 0 best solutions below