java - Spring: request-payload with only one element -
i want implement web-service consumes 1 named parameter in request-payload. in curl view should smth like: curl -x patch myurl.net/my_service -d "{mysingleparameter: 49}"
i'm trying spring, wondered map such payload method must declare new class. like:
... public static class payloadwithsingleparammsp{ public long mysingleparameter; } @requestmapping(value = "my_service", method = requestmethod.patch) public string myservice(@requestbody payloadwithsingleparammsp payload){ long valuewhichireallyneed = payload.mysingleparameter; //do job ... } ...
but there way take value need (mysingleparameter) directly?
you have couple options:
@requestmapping(value = "my_service", method = requestmethod.patch) public string myservice(@requestbody objectnode payload){ long valuewhichireallyneed = payload.get("mysingleparameter").aslong(); //do job ... }
or
@requestmapping(value = "my_service", method = requestmethod.patch) public string myservice(@requestbody map<string, string> payload){ long valuewhichireallyneed = long.parselong(payload.get("mysingleparameter")); //do job ... }
or even
@requestmapping(value = "my_service", method = requestmethod.patch) public string myservice(@requestbody long mysingleparameter){ long valuewhichireallyneed = mysingleparameter; //do job // ... }
but in last case curl following:
curl -x patch myurl.net/my_service -d "49"
in answers question can find more options: passing multiple variables in @requestbody spring mvc controller using ajax
Comments
Post a Comment