我正在使用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枚举声明中的枚举的第一个参数。到目前为止,一切都很好,但是,当我通过时在上面的URL中,sortBy = createdTs(注意小写字母c)不起作用,我试图找出在哪里使用equalsIgnoreCase()方法,它将在FinancialTransactionSortableField类中的某个位置,还是在此行上的DefaultSortableField .CREATED_TS.getCode()

最佳答案

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

08-03 20:45