ElasticSearch自定义插件无法从post请求参数中获取

时间:2015-08-11 11:28:43

标签: post curl elasticsearch elasticsearch-plugin

您好我正在为弹性搜索编写自定义插件, 但是我无法从帖子请求中获取参数。

    @Inject
public HelloRestHandler(Settings settings, RestController restController, Client esClient) {
    super(settings, restController, esClient);
    restController.registerHandler(RestRequest.Method.GET, "/_hello", this);
    restController.registerHandler(RestRequest.Method.POST, "/_hello", this);
    restController.registerHandler(RestRequest.Method.PUT, "/_hello", this);
}

@Override
public void handleRequest(final RestRequest request, final RestChannel channel, Client esClient) {
    ObjectMapper mapper = new ObjectMapper();

    String json;
    try{json=  mapper.writeValueAsString(request.params());}catch (Exception exp){ json ="not found";}
    channel.sendResponse(new BytesRestResponse(OK,json));}

卷曲:

curl -XPOST "http://localhost:9200/_hello/" -d '{"first":"1"}'

回复:

"{}" (empty JSON)

请帮我解决我的问题。感谢。

1 个答案:

答案 0 :(得分:1)

request.params()返回查询字符串中传递的HTTP参数。在你的情况下,没有。请尝试使用request.content()

String json;
try{
    json = mapper.writeValueAsString(request.content());
} catch (Exception exp){ 
    json ="not found";
}
channel.sendResponse(new BytesRestResponse(OK,json));