在查詢類型的時候定義了一個枚舉EnumTest,這樣前端傳類型的時候傳枚舉就可以。
然後在查詢的時候報錯:
JSON parse error:Cannot deserialize value of type `com.test.EnumTest` from String "ELERTRONIC": value not one of declared Enum instance names: [ELERTRONIC, GENERAL]; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `com.test.EnumTest` from String "ELERTRONIC": value not one of declared Enum instance names: [ELERTRONIC, GENERAL] at [Source: (PushbackInputStream); line: 1, column: 148] (through reference chain: com.test.FindParam["type"])
火狐瀏覽器自動格式化了入參,顯示的入參也沒有問題。枚舉定義如下:
public enum EnumTest {
/**
* 普通
**/
GENERAL(1, "普通"),
/**
* 電子
**/
ELERTRONIC(2, "電子"),;
/**
* value值,存入數據庫值
*/
private int code;
/**
* 描述
*/
private String text;
EnumTest(int code, String text) {
this.code = code;
this.text = text;
}
//根據value值獲取對應枚舉值
public EnumTest getByCode(Integer code) {
if (null == code)
return null;
for (EnumTest item : values()) {
if (code == item.getCode()) {
return item;
}
}
return null;
}
/*@JsonCreator
public static EnumTest jsonCreator(String name) {
if (StringUtils.isNotBlank(name)) {
for (EnumTest item : values()) {
if (item.name().equals(name.trim())) {
return item;
}
}
}
return null;
}*/
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
最後發現問題是前端入參傳值問題,前端傳入的枚舉爲:"ELERTRONIC ",也就是枚舉後面增加了一個空格(這裏爲了凸顯問題,實際上是多個空格)。
入參問題需要處理,最後將代碼中註釋解開,處理枚舉,解決此問題。