Spring RestController反序列化Requestbody Enum

时间:2018-04-23 15:10:56

标签: angularjs spring rest web

我有这个枚举:

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标记

1 个答案:

答案 0 :(得分:0)

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