我想我的第一个Java RESTful Web服务,也许我没有明确的机制。
这里我的代码示例:
@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;
}
我的问题是:
我应该如何通过元数据到Web服务? 我试着用
curl -g -X PUT 'http://localhost:8080/geocon/users?id=007&metadata={"name":{"first":"james","last":"bond"}}'
但我遇到这样的错误
根本原因:java.net.URISyntaxException:?/ GEOCON /用户ID = 007&元=%7B “名”:%7B “第一”: “詹姆斯”, “最后的”: “债券” %的指数33在查询非法字符7D%7D
java.net.URI中的$ Parser.fail(URI.java:2848)
谷歌搜索的时候,我已经试过这不同的解决方案:
curl -X PUT -H "application/json" -d '{"name":{"first":"james","last":"bond"}}' http://localhost:8080/geocon/users/
但这种做法,我不知道如何传递到Web服务我的意志更新与ID 007的用户(因为,据我所知,我只是传达{“名”:{“第一”:“詹姆斯”, “最后一个”: “债券”}})。
你会怎么做? 谢谢!