Request interface
The interfaces are as follows:
@GetMapping()
@apiOperation (" Query App list ")
public List<AppVO> queryApps(
@RequestParam(required = false) Long id,
@RequestParam(required = false) String name,
@RequestParam(required = false) AppSpace space,
@RequestParam(required = false) Integer platform,
@RequestParam(required = false) Integer productId) {}Copy the code
Where AppSpace is an enumeration, defined as follows (listing only code related to JSON serialization) :
@JsonCreator
public static AppSpace findByString(String name) {
for (AppSpace item : AppSpace.values()) {
if (item.name.equals(name)) {
returnitem; }}return null;
}
@JsonValue
public String toString(a) {
return this.name;
}
Copy the code
Error message
{
"message": "Failed to convert value of type 'java.lang.String' to required type 'AppSpace'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam AppSpace] for value 'space'; nested exception is java.lang.ClassCastException: AppSpace cannot be cast to java.util.Optional"
}
Copy the code
The solution
Let AppSpace use the @JsonCreator annotated antisequence method and return Optional as follows:
@JsonCreator
public static Optional<AppSpace> findByString(String name) {
for (AppSpace item : AppSpace.values()) {
if (item.name.equals(name)) {
returnOptional.of(item); }}return Optional.empty();
}
Copy the code