[英]Returning error list in jersey rest service
In our project we are using rest service(Jersey). 在我们的项目中,我们使用休息服务(泽西岛)。 In one of the requirement I would like to return a list of missing mandatory parameters back to the client. 在一项要求中,我想将丢失的必需参数列表返回给客户端。 Right now I am using exception mapper which can return a single error message, 现在,我正在使用异常映射器,它可以返回一条错误消息,
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 setters ..getter设置器
If I find any missing mandatory params, right now I am doing something like, 如果我发现任何遗漏的强制性参数,现在我正在做类似的事情,
if (emailID == null) {
throw new MandatoryParameterMissingException("Email id is missing");
}
Could some one please suggest What is the best way to enhance this to take a list of error messages and pass it back to the client? 可以请一些人提出什么建议来增强此功能,以获取错误消息列表并将其传递回客户端吗?
For this issue i created a class called ValidationException. 对于这个问题,我创建了一个名为ValidationException的类。 A ValidationException contains a list of ValidationError. ValidationException包含ValidationError的列表。
There are different kind of ValidationError, for example OutOfRangeError, MandatoryParameterError and whatever you need. 有不同类型的ValidationError,例如OutOfRangeError,MandatoryParameterError和您需要的任何东西。 A ValidationError always contains specific attributes (all what the client needs to create a meaningful messagetext for the user) => A MandatoryParameterError just the name of the field, an OutOfRangeError the name of the field and the allowed range. ValidationError始终包含特定的属性(客户端为用户创建有意义的消息文本所需的所有属性)=> MandatoryParameterError只是字段名称,OutOfRangeError字段名称和允许的范围。
On serverside there are validators (in a common package, so the client could do the validation itself)... the validators are creating ValidationErrors during the validation of your data. 在服务器端有验证器(在一个通用包中,因此客户端可以自己进行验证)...验证器在验证数据期间创建ValidationErrors。 If there are validation errors, a ValidationException is thrown. 如果存在验证错误,则抛出ValidationException。 That's it. 而已。 I can give you some codesnippets... 我可以给你一些代码片段...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.