简体   繁体   English

ElasticSearch自定义插件无法从发布请求参数获取

[英]ElasticSearch custom plugin unable to get from post request parameters

Hi I am writing custom plugin for elastic search, but I unable to get the parameter from the post request. 嗨,我正在编写用于弹性搜索的自定义插件,但无法从发布请求中获取参数。

    @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));}

The curl: 卷曲度:

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

response : 回应:

"{}" (empty JSON)

Please help me out to fix my issue. 请帮助我解决我的问题。 Thanks. 谢谢。

request.params() returns the HTTP parameters passed in the query string. request.params()返回在查询字符串中传递的HTTP参数。 In your case, there are none. 就您而言,没有。 Try with request.content() instead. 尝试使用request.content()代替。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM