【发布时间】:2018-04-23 15:10:56
【问题描述】:
我有这个枚举:
public enum DeckSuit {
SPANISH_SUIT("SpanishSuit"),
FRENCH_SUIT("FrenchSuit");
private final String text;
private DeckSuit(final String text) {
this.text = text;
}
@Override
public String toString() {
return text;
}
}
以及Controller中的这个方法:
@RequestMapping( method = RequestMethod.POST)
public int createNewGame(@RequestBody DeckSuit deckSuit) {
return gameDao.createNewGame(deckSuit);
}
从客户端,我使用 angularJS 来使用上面的方法和 http 服务,如下所示:
var deferred = $q.defer();
$http({
method : "POST",
url : RestLocationService.restlocation + resource ,
data : { deckSuit : "SPANISH_SUIT" },
headers : {
'Content-Type' : 'application/json'
}
}).then(function successCallback(response) {
console.log("Submit Success");
var responseData = response.data;
deferred.resolve(responseData);
}, function errorCallback(response) {
console.log("Submit Error");
deferred.reject();
});
return deferred.promise ;
但是我在服务器中遇到了这个异常:
.w.s.m.s.DefaultHandlerExceptionResolver : 未能读取 HTTP 信息: org.springframework.http.converter.HttpMessageNotReadableException: 无法读取文档:无法反序列化 models.utils.DeckSuit 没有 START_OBJECT 令牌
【问题讨论】:
-
您可以尝试在您的
DeckSuit类中添加一个公共默认构造函数吗? -
可能重复:stackoverflow.com/questions/36943705/… 简而言之:尝试在进程中使用POJO类,该类具有此枚举作为字段属性。