简体   繁体   中英

How to show exception message in postman?

I am getting list of products from DB using REST Webservice Call & checking if products are NULL or NOT.

IF there are no products i need to throw an exception in POSTMAN.

Can anyone throw some light on how to show exception messages in postman?

Code:

public class ABC extends BusinessException
{
    public ABC(final String message)
    {
        super(message);
    }

    public ABC(final String message, final Throwable cause)
    {
        super(message, cause);
    }
}

you can directly use WebApplicationException from jax-rs to throw the exception

For Example:

if(products==null){
 throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).entity("products does not exist.").build());
}

If you have your custom exception then you can extends WebApplicationException

public class BusinessException extends WebApplicationException {
     public BusinessException(Response.Status status, String message) {
         super(Response.status(status)
             .entity(message).type(MediaType.TEXT_PLAIN).build());
     }
}

throw from your code

 if(products==null){
      throw new BusinessException(Response.Status.NOT_FOUND,"products does not exist.");
 }

you can use error response object to display clean way

public class ErrorResponse {
  private int status;
  private String message;
  ErrorResponse(int status,String message){
     this.status = status;
     this.message = message;
  }
//setters and getters here
}

create ErrorResponse object while throwing the exception

public class BusinessException extends WebApplicationException {
     public BusinessException(Response.Status status, String message) {
         super(Response.status(status)
             .entity(new ErrorResponse(status.getStatusCode(),message)).type(MediaType.APPLICATION_JSON).build());
     }
}

In postman it will display like below

{
  status:404,
  message:"products does not exist."
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM