lundi 23 avril 2018

Spring RestController deserialize Requestbody Enum

I have this Enum:

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;
    }
}

And this method in Controller:

@RequestMapping( method = RequestMethod.POST)
public int createNewGame(@RequestBody DeckSuit deckSuit) {
    return gameDao.createNewGame(deckSuit);  
}

From the client, I am using angularJS to consume the above method with http service like this:

    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 ;

But I am getting this exception in server:

.w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of models.utils.DeckSuit out of START_OBJECT token




Aucun commentaire:

Enregistrer un commentaire