先看效果
依赖
<parent>
<groupId>org.noear</groupId>
<artifactId>solon-parent</artifactId>
<version>2.8.5</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-api</artifactId>
</dependency>
<!-- 这两个依赖二选一即可 -->
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon.validation</artifactId>
</dependency>
</dependencies>
配置类 (ValidatorFailureFilter)
import org.noear.solon.annotation.Component;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Filter;
import org.noear.solon.core.handle.FilterChain;
import org.noear.solon.core.handle.Result;
import org.noear.solon.validation.ValidatorException;
@Component
public class ValidatorFailureFilter implements Filter {
@Override
public void doFilter(Context ctx, FilterChain chain) throws Throwable {
try {
chain.doFilter(ctx);
} catch (ValidatorException e) {
//v1.10.4 后,添加 getCode() 接口
ctx.render(Result.failure(e.getCode(), e.getMessage()));
}
}
}
配置类 (ValidatorFailureHandlerImpl 可选,建议增加)
import org.noear.solon.Utils;
import org.noear.solon.annotation.Component;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Result;
import org.noear.solon.validation.ValidatorException;
import org.noear.solon.validation.ValidatorFailureHandler;
import java.lang.annotation.Annotation;
@Component
public class ValidatorFailureHandlerImpl implements ValidatorFailureHandler {
@Override
public boolean onFailure(Context ctx, Annotation anno, Result rst, String message) throws Throwable {
if (Utils.isEmpty(message)) {
if (Utils.isEmpty(rst.getDescription())) {
message = new StringBuilder(100)
.append("@")
.append(anno.annotationType().getSimpleName())
.append(" verification failed")
.toString();
} else {
message = new StringBuilder(100)
.append("@")
.append(anno.annotationType().getSimpleName())
.append(" verification failed: ")
.append(rst.getDescription())
.toString();
}
}
//这里也可以直接做输出,不过用异常更好
throw new ValidatorException(rst.getCode(), message, anno, rst);
}
}
实体类
实体类1
import com.yy.tool.otherSys.dto.OtherSysDTO;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.noear.solon.validation.annotation.Min;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Validated;
@Data
public class PageQueryParam {
/**
* 分页参数,不会出现在数据库
*/
@Min(value = 1, message = "页码不能小于1!")
@ApiModelProperty(value = "页码", required = true)
private int currentPage;
@Min(value = 1, message = "查询个数不能小于!")
@ApiModelProperty(value = "个数", required = true)
private int pageSize;
@ApiModelProperty(value = "其它系统参数", required = true)
@Validated
@NotNull(message = "系统参数不可为空!")
private OtherSysDTO sysDTO;
}
实体类2
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.noear.solon.validation.annotation.NotBlank;
import java.io.Serializable;
@Data
public class OtherSysDTO implements Serializable {
@ApiModelProperty(value = "系统凭证", required = true)
@NotBlank
private String token;
@ApiModelProperty(value = "系统名称", required = true)
@NotBlank(message = "系统名称不能空!")
private String sysName;
@ApiModelProperty(value = "系统环境", required = true)
@NotBlank(message = "系统环境不能空!")
private String env;
}
控制层
需要注意的是控制层类需要加@Valid,然后方法也要加@Validated注解
@Valid
@Mapping("/test")
@Controller
public class TestController{
@Post
@Mapping("list")
public void list(@Validated @Body PageQueryParam param) {
//你自己的业务
}
}
完了!