java - Get simple JSON Parameter from a JSON request in JAX-RS -
the client/browser makes json request rest resource (the content-type of request application/json
, corresponding rest method @consumes("application/json")
annotated).
@path("/process-something") @post @produces("application/json") @consumes("application/json") @handledefaultexceptions public aresponse processsomething(list<long>) { }
the json body consists of simple types, list<long>
or string
.
is there simple possibility json parameters injected annotating somehow, similar @formparam
in case of application/x-www-form-urlencoded
request? other easier solutions decoding json string jackson's objectmapper
or jettison's jsonobject
.
you may create java class reflects data model of json object , annotate jaxb's @xmlrootelement. can map attributes custom json key name @xmlelement annotations, e.g.:
@xmlrootelement public class myjsonoject{ @xmlelement(name="json-key-name") public string attribute; }
then jersey can decode json object transparently , voila!
@path("/process-something") @post @produces("application/json") @consumes("application/json") public aresponse processsomething(myjsonoject json) { log.fine(json.attribute); }
Comments
Post a Comment