我试图弄清楚为什么FinalTransactionSortableField.fromCode(orderBy)
的值出现为null。我正在使用一个REST客户端
参数将传递给以下控制器方法。
我的控制器类中有以下方法:
public ResponseEntity<SetRESTData<FinancialTransactionDTO>> getTFIUse(
@RequestParam(value = "bId", required = true) final String bId,
@RequestParam(value = "status", required = false) final String statusString,
@RequestParam(value = "transactionMethod", required = false) final String transactionMethodString,
@RequestParam(value = "sortBy", required = false) final String orderBy,
@RequestParam(value = "sortDirection", required = false) final String sortDirectionString) {
//line 76 below
final FinancialTransactionPaginationData paginationData = StringUtils.isBlank(orderBy) ?
null : new FinancialTransactionPaginationData(FinancialTransactionSortableField.fromCode(orderBy), null, null, sortDirectionString);
//line# 79 below
final ServiceResultDetails<Set<FinancialTransactionDTO>> resultsDetails =
financialTransactionService.getTransactionsForEmployeeUse(businessId,status, method, paginationData);
}
当我在第79行放置一个断点时,我可以看到分页数据为field
返回null,如下面的截图所示,来自eclipse中的调试器:
The FinalTransactionSortableField class has`fromCode()` method defined as follows :
public static FinancialTransactionSortableField fromCode(final String code) {
// Line #61 below
return CODE_MAP.get(code);
}
当我将调试点放在第61行(return语句)时,我可以在eclipse中看到调试器上的值createdTs
,如下所示:
供参考:
1)CODE_MAP
是在FinancialTransactionSortableField类中定义的Map,如下所示:
public enum FinancialTransactionSortableField implements MappableEnum<FinancialTransactionSortableField>, SortableField {
ID(DefaultSortableField.ID.getCode(), "Transaction Id", DefaultSortableField.ID.getColumnName()),
CASH_AMOUNT("CashAmount", "Cash Amount", "cashAmount"),
SECURITY_AMOUNT("SecurityAmount", "Security Amount", "securityAmount"),
STATUS("Status", "Status", "status"),
CREATED_TS(DefaultSortableField.CREATED_TS.getCode(),
DefaultSortableField.CREATED_TS.getDescription(),
DefaultSortableField.CREATED_TS.getColumnName()),;
private final String code;
private final String description;
private final String columnName;
private static final Map<String, FinancialTransactionSortableField> CODE_MAP =
MappableEnumUtil.buildCodeMap(FinancialTransactionSortableField.class);
private FinancialTransactionSortableField(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 FinancialTransactionSortableField toEnum(final String code) {
return fromCode(code);
}
@JsonCreator
public static FinancialTransactionSortableField fromCode(final String code) {
return CODE_MAP.get(code);
}
}
2)buildCodeMap
方法的实现如下:
public static <E extends Enum<E> & MappableEnum<E>> Map<String, E> buildCodeMap(final Class<E> enumClass) {
return buildLookupMap(enumClass,
new EnumValueGetter<E>() {
@Override
public Set<String> getKeys(final E e) {
final Set<String> set = new LinkedHashSet<String>(1);
set.add(e.getCode());
return set;
}
});
}
3)以下是buildLookupMap
实施
public static <E extends Enum<E> & MappableEnum<E>> Map<String, E> buildLookupMap(
final Class<E> enumClass, final EnumValueGetter<E> getter) {
final Set<E> values = EnumSet.allOf(enumClass);
final E enumValue = values.iterator().next();
addToExampleMap(enumClass, enumValue);
final Map<String, E> lookupMap = new LinkedHashMap<>(values.size());
for (final E value : values) {
final Set<String> keys = getter.getKeys(value);
for (final String key : keys) {
if (lookupMap.containsKey(key)) {
throw new FPIllegalArgumentException("Lookup map for {} has duplicate key {}", enumClass.getSimpleName(), key);
} else {
lookupMap.put(key, value);
}
}
}
return lookupMap;
}
4)addToExampleMap
方法和EnumValueGetter
接口的实现如下所示:
private static synchronized <E extends Enum<E> & MappableEnum<E>> void addToExampleMap(
final Class<E> enumClass, final E enumValue) {
if (!EXAMPLE_MAP.containsKey(enumClass)) {
EXAMPLE_MAP.put(enumClass, enumValue);
}
}
public static interface EnumValueGetter<E extends Enum<E> & MappableEnum<E>> {
Set<String> getKeys(E e);
}
和EXAMPLE_MAP
是一个在开头的类中声明的地图,如下所示:
private static final Map<Class<?>, MappableEnum<?>> EXAMPLE_MAP =
new ConcurrentHashMap<Class<?>, MappableEnum<?>>(1000);
有人能看出什么问题吗?如果需要更多信息,请告诉我。