How to make RESTful java client to send GET/POST request on Openstack?

廉价感情. 提交于 2019-12-08 21:20:28

I prefer Jersey API for REST calls;

Below one is to obtain token for specified user that is in your POST entity;

String postEntity = "yourJson";
JerseyClient jerseyClient = JerseyClientBuilder.createClient();
JerseyWebTarget jerseyTarget = jerseyClient.target("http://***.**.**.**:****/v2.0/tokens");
JerseyInvocation.Builder jerseyInvocation = jerseyTarget.request("application/json");
jerseyInvocation.header("Context-type", "application/json");
Response response = jerseyInvocation.post(Entity.entity(postEntity, MediaType.APPLICATION_JSON), Response.class);

Then you can parse your entity with some of parser such as com.google.gson.JsonParser.

JsonParser jsonParser = new JsonParser();
String responseEntity = jsonParser.parse(response.readEntity(String.class));

After that for each request , you need to apply X-Auth-Token to your REST header in order to authenticate for services.

jerseyInvocation.header("X-Auth-Token",token);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!