【问题标题】:Spring Boot/Java Mapping Enum Values to RequestParam [duplicate]Spring Boot / Java将枚举值映射到RequestParam [重复]
【发布时间】: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


【解决方案1】:

将您的方法更改为以下:

@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") CustomerType customerType) {
    // ...
}

即customerType 类型应该是 CustomerType 而不是 String。现在只有匹配枚举的值才会被映射。

注意:- 必须提供特定格式的值,即枚举名称本身,例如在您的情况下,FREELANCERRETAILPUBLICATION 等值应在请求中传递。

编辑

根据 OP 的要求,下面是自定义字符串的枚举处理:

在控制器中添加@initBinder并添加如下方法:

@InitBinder
    public void initBinder(final WebDataBinder webdataBinder) {
        webdataBinder.registerCustomEditor(Customer.class, new CustomerConverter());
    }

并声明一个转换器类如下:

import java.beans.PropertyEditorSupport;

public class CustomerConverter extends PropertyEditorSupport{

     public void setAsText(final String text) throws IllegalArgumentException {
         System.out.println("--->"+Customer.getContentType(text));
            setValue(Customer.getContentType(text));
        }¡¡
}

添加了System.out.println 以显示该值按预期解释和打印。

【讨论】:

  • 我希望将这些映射到零售、FreeLancer、市场、出版基本上是字符串的内部值。
  • 我已经更新了答案
  • 非常感谢,拯救了我的一天。
【解决方案2】:

一个简单的空检查就可以了

Customer customer = Customer.getContentType(customerType);
if (customer == null) {
     throw new Exception("Invalid Customer type");// use validation exception 
}

【讨论】:

  • 感谢您的回复,如果可能的话,我需要在方法声明本身中执行此操作,而不是在正文中。我想这必须在方法体中完成
猜你喜欢
  • 2019-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-09
  • 2019-01-22
  • 1970-01-01
  • 2019-04-09
  • 2019-08-14
相关资源
最近更新 更多