确定将equalsIgnoreCase()置于何处以避免区分大小写

时间:2016-01-14 05:07:03

标签: java enums

我正在使用Spring 4并使用以下URL进行API调用

我有一个Java枚举类,其定义如下:

http://localhost.com:8080/int/fin/trs?bId=B-XYZ&sortBy=currentTs

并且,我有控制器方法,此调用接收参数:

public ResponseEntity<SetRESTData<FinancialTransactionDTO>> getTFIUse(

    @RequestParam(value = "status", required = false) final String statusString,
    @RequestParam(value = "transactionMappableEnumthod", required = false) final String transactionMappableEnumthodString,
    @RequestParam(value = "sortBy", required = false) final String orderBy,
    @RequestParam(value = "sortDirection", required = false) final String sortDirectionString) {


    final FinancialTransactionPaginationData paginationData = StringUtils.isBlank(orderBy) ?
      null : new FinancialTransactionPaginationData(FinancialTransactionSortableField.fromCode(orderBy), null, null, sortDirectionString);


  }

FinancialTransactionSortableField类如下:

public enum FinancialTransactionSortableField implements MappableEnum<FinancialTransactionSortableField>, SF {

  ID(DefaultSortableField.ID.getCode(), "T Id", DefaultSortableField.ID.getColumnNaMappableEnum()),
  CREATED_TS(DefaultSortableField.CREATED_TS.getCode(),
             DefaultSortableField.CREATED_TS.getDescription(),
             DefaultSortableField.CREATED_TS.getColumnNaMappableEnum()),;

DefaultSortableField枚举类定义如下:

public enum DefaultSortableField implements MappableEnum<DefaultSortableField>, SF {


  CREATED_TS("CreatedTs", "Created Timestamp", "CreatedTs"),;

  private final String code;
  private final String description;
  private final String columnName;

  private static final Map<String, DefaultSortableField> cmap =
    MappableEnum.buildCodeMap(DefaultSortableField.class);

  private DefaultSortableField(final String code, final String description, final String columnName) {
    this.code = code;
    this.description = description;
    this.columnName = columnName;
  }

  @JsonValue
  @Override
  public String getCode() {
    return code;
  }

  @Override
  public String getDescription() {
    return description;
  }

  @Override
  public String getColumnName() {
    return columnName;
  }

  @Override
  public DefaultSortableField toEnum(final String code) {
    return fromCode(code);
  }

  @JsonCreator
  public static DefaultSortableField fromCode(final String code) {
    return cmap.get(code);
  }

  }

我基本上是在做一个这样的api调用http://localhost.com:8080/int/fin/trs?bId=B-XYZ&sortBy=CreatedTs   因此,当代码执行到达FinancialTransactionSortableField类的方法DefaultSortableField.CREATED_TS.getCode()时,将返回CreatedTs值(这是DefaultSortableField CREATED_TS枚举声明中枚举的第一个枚举参数。到目前为止,一切都很好。但是,当我通过时sortBy = createdTs(注意小写c)在上面的URL中,它不起作用。我试图弄清楚在哪里使用equalsIgnoreCase()方法,它是否会在FinancialTransactionSortableField类中的某处,在此行的某处DefaultSortableField .CREATED_TS.getCode()

1 个答案:

答案 0 :(得分:1)

@JsonCreator
public static DefaultSortableField fromCode(final String code) {
    for(String key : cmap.keySet()){
        if(code.equalsIgnoreCase(key)){
            return cmap.get(key);
        }
    }
    return null;
}