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

ElasticSearch custom plugin unable to get from post request parameters

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

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

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

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

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