【问题标题】:Spring RestController deserialize Requestbody EnumSpring RestController 反序列化 Requestbody 枚举
【发布时间】: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类,该类具有此枚举作为字段属性。

标签: angularjs spring rest web


【解决方案1】:

return gameDao.createNewGame(deckSuit);
- 尝试调用deckSuit.toString()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 2015-07-26
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 2016-09-16
    • 2014-08-10
    相关资源
    最近更新 更多