/**
* 定义一个枚举
*
*/
@Getter
public enum StatusType {
SUCCESS("1","成功"),
FAIL("2","失败"),
PROCESSING("3","进行中"),
;
StatusType(String code,String desc){
this.code = code;
this.desc = desc;
}
private String code;
private String desc;
public static StatusType getEnum(String code){
return Stream.of(values())
.filter(a -> a.code.equals(code))
.findFirst()
.orElseThrow(UnsupportedOperationException::new);
}
}
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/*
* 编写config配置
*/
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new StatusTypeConverter());
}
}
import org.springframework.core.convert.converter.Converter;
/**
* 编写转换器
*/
public class StatusTypeConverter implements Converter<String, StatusType> {
@Override
public StatusType convert(String source) {
return StatusType.getEnum(source);
}
}
具体使用直接将controller层入参的status用StatusType接收即可。
@NotNull(message = "入参状态字段不能为空!")
private StatusType statusCode;