jersey (JSR-311), how to implement @POST in container pattern

Question!

from netbeans, i created a new REST webservice (using jersey), using the built-in wizards. in the container resource class, it created a 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 to i POST to this? my understanding is that in need to post name=val pairs. what's jersey expecting here? how would i post json to this using say curl? here's what i 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 i post this, and look at the identity object coming in, all fields are null? i suspect my json is incorrect. when i manually add an object to my container, then form a get, i see this result,

{"identities":{"id":"Foo Bar","vcard":"VCARD123","location":{"latitude":"-1.0","longitude":"-1.0"}}}

when i try to post the same thing, all fields all null. i also tried,

{"id":"Foo Bar","vcard":"VCARD123","location":{"latitude":"-1.0","longitude":"-1.0"}}

same result. ? thanks.

thanks.



Answers

To send requests to this method using curl, you would have to use something like:

HEADER='--header Content-Type:application/json'
URL='http://localhost:
By : cheekoo


This video can help you solving your question :)
By: admin