在开发过程中,有时候需要对某些字段进行不同场景的校验,例如id的校验,我只需要在修改,删除和查询的时候需要校验不能为空,而像某些名称的字段,可能只需要对它进行添加,修改的场景下进行校验,那么这种多场景下该如何对字段进行校验呢?下面就来简单演示
1.首先是控制层
/**
* 添加LRP汇率
*/
@PostMapping("/save")
public ResponseData save(@RequestBody @Validated(TBaseCommonLrpParam.add.class) TBaseCommonLrpParam lrpParam) {
tBaseCommonLrpService.save(lrpParam);
return new SuccessResponseData();
}
以上是添加的校验分组,然后我们看看编辑的
/**
* 编辑LRP汇率
*/
@PostMapping("/edit")
public ResponseData edit(@RequestBody @Validated(TBaseCommonLrpParam.edit.class) TBaseCommonLrpParam lrpParam) {
tBaseCommonLrpService.edit(lrpParam);
return new SuccessResponseData();
}
只需要在@validated注解里加上对象的分组后缀即可,接下来我们看看在模型对象中该怎么处理
2.模型对象
public class TBaseCommonLrpParam extends BaseParam {
@NotNull(message = "id不能为空,请检查id参数", groups = {edit.class, delete.class, detail.class})
private Long id;
/**
* 1年期利率
*/
@NotNull(message = "1年期利率不能为空,请检查oneYearRate参数", groups = {add.class,edit.class})
private BigDecimal oneYearRate;
......
3.继承的BaseParam类
import java.io.Serializable;
import java.util.List;
public class BaseParam implements Serializable {
private static final long serialVersionUID = 1L;
private String searchValue;
private List<Long> dataScope;
private String searchBeginTime;
private String searchEndTime;
private Integer searchStatus;
public BaseParam() {
}
public String getSearchValue() {
return this.searchValue;
}
public List<Long> getDataScope() {
return this.dataScope;
}
public String getSearchBeginTime() {
return this.searchBeginTime;
}
public String getSearchEndTime() {
return this.searchEndTime;
}
public Integer getSearchStatus() {
return this.searchStatus;
}
public void setSearchValue(String searchValue) {
this.searchValue = searchValue;
}
public void setDataScope(List<Long> dataScope) {
this.dataScope = dataScope;
}
public void setSearchBeginTime(String searchBeginTime) {
this.searchBeginTime = searchBeginTime;
}
public void setSearchEndTime(String searchEndTime) {
this.searchEndTime = searchEndTime;
}
public void setSearchStatus(Integer searchStatus) {
this.searchStatus = searchStatus;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof BaseParam)) {
return false;
} else {
BaseParam other = (BaseParam)o;
if (!other.canEqual(this)) {
return false;
} else {
label71: {
Object this$searchValue = this.getSearchValue();
Object other$searchValue = other.getSearchValue();
if (this$searchValue == null) {
if (other$searchValue == null) {
break label71;
}
} else if (this$searchValue.equals(other$searchValue)) {
break label71;
}
return false;
}
Object this$dataScope = this.getDataScope();
Object other$dataScope = other.getDataScope();
if (this$dataScope == null) {
if (other$dataScope != null) {
return false;
}
} else if (!this$dataScope.equals(other$dataScope)) {
return false;
}
label57: {
Object this$searchBeginTime = this.getSearchBeginTime();
Object other$searchBeginTime = other.getSearchBeginTime();
if (this$searchBeginTime == null) {
if (other$searchBeginTime == null) {
break label57;
}
} else if (this$searchBeginTime.equals(other$searchBeginTime)) {
break label57;
}
return false;
}
Object this$searchEndTime = this.getSearchEndTime();
Object other$searchEndTime = other.getSearchEndTime();
if (this$searchEndTime == null) {
if (other$searchEndTime != null) {
return false;
}
} else if (!this$searchEndTime.equals(other$searchEndTime)) {
return false;
}
Object this$searchStatus = this.getSearchStatus();
Object other$searchStatus = other.getSearchStatus();
if (this$searchStatus == null) {
if (other$searchStatus == null) {
return true;
}
} else if (this$searchStatus.equals(other$searchStatus)) {
return true;
}
return false;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof BaseParam;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $searchValue = this.getSearchValue();
int result = result * 59 + ($searchValue == null ? 43 : $searchValue.hashCode());
Object $dataScope = this.getDataScope();
result = result * 59 + ($dataScope == null ? 43 : $dataScope.hashCode());
Object $searchBeginTime = this.getSearchBeginTime();
result = result * 59 + ($searchBeginTime == null ? 43 : $searchBeginTime.hashCode());
Object $searchEndTime = this.getSearchEndTime();
result = result * 59 + ($searchEndTime == null ? 43 : $searchEndTime.hashCode());
Object $searchStatus = this.getSearchStatus();
result = result * 59 + ($searchStatus == null ? 43 : $searchStatus.hashCode());
return result;
}
public String toString() {
return "BaseParam(searchValue=" + this.getSearchValue() + ", dataScope=" + this.getDataScope() + ", searchBeginTime=" + this.getSearchBeginTime() + ", searchEndTime=" + this.getSearchEndTime() + ", searchStatus=" + this.getSearchStatus() + ")";
}
public @interface deleteSign {
}
public @interface addSign {
}
public @interface changeStatus {
}
public @interface commentHistory {
}
public @interface change {
}
public @interface mapping {
}
public @interface export {
}
public @interface end {
}
public @interface back {
}
public @interface submit {
}
public @interface jump {
}
public @interface trace {
}
public @interface turn {
}
public @interface entrust {
}
public @interface active {
}
public @interface suspend {
}
public @interface deploy {
}
public @interface start {
}
public @interface stop {
}
public @interface force {
}
public @interface grantData {
}
public @interface grantMenu {
}
public @interface grantRole {
}
public @interface detail {
}
public @interface delete {
}
public @interface updateAvatar {
}
public @interface resetPwd {
}
public @interface updatePwd {
}
public @interface updateInfo {
}
public @interface edit {
}
public @interface add {
}
public @interface dropDown {
}
public @interface list {
}
public @interface page {
}
}
这样我们就可以对一个字段在多种场景下进行不同的校验了
接下来我们看看时间转换的问题,在某些情况下,时间对象会被自动生成为
LocalDateTime类型的时间对象,常规的则是Date这个类型的,那么这种情况我们处理的方式有2种
首先第一种就是通过注解来完成:
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
另外一种方式是增加一个配置类来完成,在某些情况下以上的注解会不起作用
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.format.DateTimeFormatter;
/**
* LocalDateTime全局格式
*/
@Configuration
public class LocalDateTimeGlobalConfig {
private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
/**
* 配置LocalDateTime类型序列化与反序列化
*/
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
/*return new Jackson2ObjectMapperBuilderCustomizer() {
@Override
public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
jacksonObjectMapperBuilder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)));
jacksonObjectMapperBuilder.deserializers(new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)));
}
};*/
//这种方式等同于上边
return builder -> {
builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)));
builder.deserializers(new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)));
};
}
}
这样是增加了一个localDateTime类型处理的全局配置类