I'm trying my first Java RESTful web service and probably I don't have clear some mechanisms.
Here an example of my code:
@Path(Paths.USERS)
public class UserService {
private static final String OK_MESSAGE_USERSERVICE_PUT = Messages.OK_MESSAGE_USERSERVICE_PUT;
private Client esClient = ElasticSearch.getClient();
@GET
@Produces(MediaType.APPLICATION_JSON)
public String get(@QueryParam(QParams.ID) String id) {
// TODO Authentication
try {
GetResponse response = esClient
.prepareGet(PRIMARY_INDEX_NAME, USERS_TYPE_NAME, id)
.execute().actionGet();
if (response != null) {
return response.getSourceAsString();
}
}catch (ElasticsearchException e) {
e.printStackTrace();
return e.getMessage();
}
return Messages.RESOURCE_NOT_FOUND;
}
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public String update(@QueryParam(QParams.ID) String id,
@PathParam("metadata") String metadata) {
// TODO Authentication
boolean isMyself = true;
// precondition, the user exsists. If the check fails, you
// should put the isMyself flag at false.
if (isMyself){
esClient
.prepareIndex(PRIMARY_INDEX_NAME, USERS_TYPE_NAME, id)
.setSource(metadata).execute().actionGet();
}
return OK_MESSAGE_USERSERVICE_PUT;
}
My problem is:
How should I pass the metadata to the web service? I tried with
curl -g -X PUT 'http://localhost:8080/geocon/users?id=007&metadata={"name":{"first":"james","last":"bond"}}'
but I encounter an error like
Root Cause: java.net.URISyntaxException: Illegal character in query at index 33: /geocon/users?id=007&metadata=%7B"name":%7B"first":"james","last":"bond"%7D%7D
java.net.URI$Parser.fail(URI.java:2848)
Googling around, I've tried this different solution:
curl -X PUT -H "application/json" -d '{"name":{"first":"james","last":"bond"}}' http://localhost:8080/geocon/users/
but with this approach, I don't know how to pass to the web service my will of updating the user with ID 007 (since, AFAIK, I'm only communicating {"name":{"first":"james","last":"bond"}}).
How would you do? Thanks!