【发布时间】:2019-08-01 12:20:15
【问题描述】:
我有一个像下面这样的枚举
public enum Customer {
RETAIL("retail"),
FREELANCER("FreeLancer"),
MARKET("market"),
PUBLICATION("publication");
private String contentType;
private static final Map<String,Customer> contentTypeMap;
public String getContentType(){
return this.contentType;
}
static {
Map<String,Customer> map = new ConcurrentHashMap<>();
for(Customer type : Customer.values ()){
map.put (type.getContentType (),type);
}
contentTypeMap = map;
}
Customer(String contentType){
this.contentType=contentType;
}
public static Customer getContentType(String contentType){
return contentTypeMap.get (contentType);
}
}
这个枚举代表客户的类型。
我们有一个返回客户详细信息的 API
@RequestMapping(value="/getData", method=RequestMethod.GET, produces="application/json")
public BatchResponse triggerBatchJob(
@RequestParam(value="updateFrom", required=false) @DateTimeFormat(pattern="yyyyMMdd") String updateFrom,
@RequestParam(value="updateTo", required=false) @DateTimeFormat(pattern="yyyyMMdd") String updateTo,
@RequestParam(value="customerType") (VALIDATE_HERE)String customerType) {
// ...
}
我需要验证 customerType 值是否存在于 Enum 中,有没有办法通过使用 getContentType 方法或其他方法来验证方法声明与我在日期而不是方法体的情况下所做的相同.
请帮忙。
【问题讨论】:
-
Spring 已经为您验证过,即如果您传递任何与可用值不匹配的值,则此端点将返回错误消息。如果您正在寻找为您的应用程序定制的特殊验证,请说明您想要什么类型的附加验证。
标签: spring-boot spring-mvc java-8 request-mapping