我正在使用drop-wizard开发一个Web服务,该服务使用Jackson进行JSON,该服务接受JSON格式的数据(ES中的文档),我需要将其传递给ES对其进行索引。由于这是大型JSON,目前我没有相应的JAVA POJO,也没有对其进行反序列化,而是直接将其发送到Elastic-search(ES)api,该API也需要JSON格式的数据。
但是现在我正在调用Java高级客户端来索引文档,这要求根据提供的代码示例https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.1/java-rest-high-document-index.html
将此JSON转换为String格式。IndexRequest request = new IndexRequest("posts");
request.id("1");
String jsonString = "{" +
"\"user\":\"kimchy\"," +
"\"postDate\":\"2013-01-30\"," +
"\"message\":\"trying out Elasticsearch\"" +
"}";
request.source(jsonString, XContentType.JSON);
现在,由于已经有 JSON 进入我的Web服务资源,我所需要做的就是将其转换为String并调用该服务,但我无法正确处理它。
我的资源代码
@POST
@Path("{id}")
public Response create(@PathParam ("id") String id, @NotNull JsonNode json){
try {
service.add(id,json);
URI uri = new URI(id);
return Response.created(uri).build();
}catch (Exception e){
log.error("Error indexing doc", e);
}
}
还有我的服务代码
String jsonStr = json.textValue();
请帮帮我,因为我对JSON解析不了解,基本上我的要求是将来自Jersey资源的JSON转换为String。
答案 0 :(得分:1)
使用public void GetData(final String user_id){
String info=user_id;
final ViewDialog alert = new ViewDialog(getContext());
Log.e("info",info);
G.getMyAPI().User_Post(info).enqueue(new Callback<List<Model>>() {
@Override
public void onResponse(Call<List<Model>> call, Response<List<Model>> response) {
layout.setRefreshing(false);
if (response.isSuccessful()){
Log.e("response",String.valueOf(response.body().get(0).getId()));
if (response.body().size()>0){
size=response.body().size();
adapter = new Products_Adapter(getFragmentManager(),response.body(), getContext(),getActivity());
recyclerview.setLayoutManager(manager);
recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerview.setHasFixedSize(true);
recyclerview.setAdapter(adapter);
recyclerview.setItemViewCacheSize(20);
}
else {
alert.showDialog_error(getContext(), "Your connection to the server has been disconnected");
}
// layout.setRefreshing(false);
}
else {
alert.showDialog_error(getContext(), "Your connection to the server has been disconnected");
}
}
@Override
public void onFailure(Call<List<Model>> call, Throwable t) {
layout.setRefreshing(false);
Log.e("Error_user_post",t.getMessage());
alert.showDialog_error(getContext(), "Your connection to the server has been disconnected");
}
});
}
的{{1}}方法解决了OP问题。