java - jersey (JSR-311), how to implement @POST in container pattern -
from netbeans, created new rest webservice (using jersey), using built-in wizards. in container resource class, created stub,
@post @consumes("application/json") @produces("application/json") public response postjson(identity identity) { identities.addidentity(identity); return response.status(status.ok).entity(identity).build(); } how post this? understanding in need post name=val pairs. what's jersey expecting here? how post json using curl? here's tried,
#!/bin/bash data="{ \"id\": \"$1\", \"vcard\": \"$2\", \"location\": { \"latitude\": \"$3\", \"longitude\": \"$4\" } }" echo "posting: $data" header='content-type:application/json' url='http://localhost:8080/contacthi-proximity-service/resources/is' curl --data-binary "${data}" -h "${header}" "${url}" when post this, , @ identity object coming in, fields null? suspect json incorrect. when manually add object container, form get, see result,
{"identities":{"id":"foo bar","vcard":"vcard123","location":{"latitude":"-1.0","longitude":"-1.0"}}} when try post same thing, fields null. tried,
{"id":"foo bar","vcard":"vcard123","location":{"latitude":"-1.0","longitude":"-1.0"}} same result. ? thanks.
thanks.
to send requests method using curl, have use like:
header='--header content-type:application/json' url='http://localhost:<port>/methodname' curl --data-binary request.json ${header} ${url} -d response.txt you can pass string method. above code pick json string file mentioned. sample json be:
{"username":"test","timestamp":"2010-08-05t11:35:32.982-0800","userid":"0982"} for creating response can use like:
return response.status(status.ok).entity(responsestring).build(); classes used are:
import javax.ws.rs.core.response; import javax.ws.rs.core.response.status;
Comments
Post a Comment