我正在使用 java 11 在 Spring Boot 中撰寫登錄 rest api,這是登錄 api 服務器端代碼:
@PostMapping("/plugin/login")
Response<LoginResponse> pluginLogin(@RequestBody UserLoginRequest request);
這是UserLoginRequest定義:
package biz.user;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import misc.enumn.DeviceType;
import misc.enumn.app.AppName;
import misc.enumn.user.LoginType;
import java.io.Serializable;
/**
* @author dolphin
*/
@Data
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserLoginRequest implements Serializable {
/**
* user.phone
*
* @mbggenerated
*/
@NonNull
private String phone;
/**
* user.password
*
* @mbggenerated
*/
@NonNull
private String password;
@NonNull
private LoginType loginType;
/**
* user.phone_region
*
* @mbggenerated
*/
@ApiModelProperty(value = "phone region")
private String phoneRegion;
@ApiModelProperty(value = "app")
@NonNull
private AppName app;
@ApiModelProperty(value = "device type")
private DeviceType deviceType;
@ApiModelProperty(value = "device type")
private String deviceId;
@ApiModelProperty(value = "nick name")
private String nickname;
@ApiModelProperty(value = "avatar url")
private String avatarUrl;
}
當我像這樣呼叫這個api時:
curl 'https://dict.example.top/dict/user/plugin/login' \
-H 'Connection: keep-alive' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4753.0 Safari/537.36' \
-H 'Content-type: application/json' \
-H 'Accept: */*' \
-H 'Origin: chrome-extension://alepiijaddmmflnaibdpolcgglgmkpdm' \
-H 'Sec-Fetch-Site: none' \
-H 'Sec-Fetch-Mode: cors' \
-H 'Sec-Fetch-Dest: empty' \
-H 'Accept-Language: en-US,en;q=0.9' \
--data-raw '{"phone":"","password":"","deviceId":"xxxx","app":7,"deviceType":7,"loginType":1}' \
--compressed
服務器顯示錯誤:
Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: app is marked non-null but is null; nested exception is com.fasterxml.jackson.databind.JsonMappingException: app is marked non-null but is null
at [Source: (PushbackInputStream); line: 1, column: 51] (through reference chain: biz.user.UserLoginRequest["app"])
為什么會這樣?我應該怎么做才能解決它?這是AppName 定義:
@Getter
public enum AppName implements BaseEnum {
CRUISE(1, "cruise"),
BACK(2, "back"),
REDDWARF_MUSIC(4, "長歌"),
REDDWARF_DICT(5, "紅矮星詞典"),
REDDWARF_ADMIN(6, "紅矮星后臺管理系統"),
;
@JsonValue
private Integer key;
private String value;
AppName(Integer key, String value) {
this.key = key;
this.value = value;
}
private static final Map<Integer, AppName> mappings;
static {
Map<Integer, AppName> temp = new HashMap<>();
for (AppName courseType : values()) {
temp.put(courseType.key, courseType);
}
mappings = Collections.unmodifiableMap(temp);
}
/**
* @param key
* @return
*/
@EnumConvertMethod
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
public static AppName resolve(Integer key) {
return mappings.get(key);
}
public void setKey(Integer key) {
this.key = key;
}
public void setValue(String value) {
this.value = value;
}
public static AppName getAppMarkByValue(String value) {
AppName datetimeType = null;
for (AppName type : AppName.values()) {
if (type.name().equals(value)) {
datetimeType = type;
}
}
return datetimeType;
}
public static AppName getAppMarkByKey(Short key) {
AppName datetimeType = null;
for (AppName type : AppName.values()) {
if (type.key.equals(key)) {
datetimeType = type;
}
}
return datetimeType;
}
}
uj5u.com熱心網友回復:
您正在使用列舉來接收應用程式引數,在客戶端上您告訴服務器您的應用程式是 7,但是在您的 AppName 列舉中您沒有定義哪個應用程式是 7。所以當決議 7 時沒有映射任何應用程式時,它會變為空。但是您的龍目島驗證應用程式不能為空,這就是您收到錯誤的原因。解決您的問題,您應該在 AppName 中添加 7 應用程式定義,如下所示:
CRUISE(1, "cruise"),
BACK(2, "back"),
REDDWARF_MUSIC(4, "長歌"),
REDDWARF_DICT(5, "紅矮星詞典"),
REDDWARF_ADMIN(6, "紅矮星后臺管理系統"),
MY_NEW_APP(7, "the new app name"),
將 替換MY_NEW_APP為您想要命名的任何內容。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/432221.html
上一篇:使用可能回傳不同資料的JSON
