繁体   English   中英

在球衣休息服务中返回错误列表

[英]Returning error list in jersey rest service

在我们的项目中,我们使用休息服务(泽西岛)。 在一项要求中,我想将丢失的必需参数列表返回给客户端。 现在,我正在使用异常映射器,它可以返回一条错误消息,

public class MandatoryParameterMissingException extends RuntimeException {

    private static final long serialVersionUID = 1L;


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


public class MandatoryParamMissingExceptionMapper implements ExceptionMapper<MandatoryParameterMissingException> {

    @Override
    public Response toResponse(MandatoryParameterMissingException ex) {
        ErrorMessage errorMessage = new ErrorMessage(ex.getMessage(), 404, "Document source:todo");
        return Response.status(Status.NOT_FOUND)
                .entity(errorMessage)
                .build();
    }

private String errorMessage;
    private int errorCode;
    private String documentation;

    public ErrorMessage() {

    }

    public ErrorMessage(String errorMessage, int errorCode, String documentation) {
        super();
        this.errorMessage = errorMessage;
        this.errorCode = errorCode;
        this.documentation = documentation;
    }

..getter设置器

如果我发现任何遗漏的强制性参数,现在我正在做类似的事情,

if (emailID == null) {
            throw new MandatoryParameterMissingException("Email id is missing");
        }

可以请一些人提出什么建议来增强此功能,以获取错误消息列表并将其传递回客户端吗?

对于这个问题,我创建了一个名为ValidationException的类。 ValidationException包含ValidationError的列表。

有不同类型的ValidationError,例如OutOfRangeError,MandatoryParameterError和您需要的任何东西。 ValidationError始终包含特定的属性(客户端为用户创建有意义的消息文本所需的所有属性)=> MandatoryParameterError只是字段名称,OutOfRangeError字段名称和允许的范围。

在服务器端有验证器(在一个通用包中,因此客户端可以自己进行验证)...验证器在验证数据期间创建ValidationErrors。 如果存在验证错误,则抛出ValidationException。 而已。 我可以给你一些代码片段...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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