Web services 在WebSphereLiberty概要文件中使用JAXRS客户端API(POST请求)的Http客户端

Web services 在WebSphereLiberty概要文件中使用JAXRS客户端API(POST请求)的Http客户端,web-services,jax-rs,websphere-liberty,cics,Web Services,Jax Rs,Websphere Liberty,Cics,我需要创建一个Http客户端,用WLP提供的JAXRS客户端API(lib:javax.ws.rs.Client.*)测试RESTWeb服务。我必须使用POST方法发送字符串请求(JSON消息)并接收字符串响应(JSON消息)。如果有人有类似的Java代码(类)和完成此任务所需的导入,我将不胜感激 PS:我开始编写Java类,但不知道如何获得响应: Client client = ClientBuilder.newClient(); WebTarget myResource = client.

我需要创建一个Http客户端,用WLP提供的JAXRS客户端API(lib:javax.ws.rs.Client.*)测试RESTWeb服务。我必须使用POST方法发送字符串请求(JSON消息)并接收字符串响应(JSON消息)。如果有人有类似的Java代码(类)和完成此任务所需的导入,我将不胜感激

PS:我开始编写Java类,但不知道如何获得响应:

Client client = ClientBuilder.newClient(); 
WebTarget myResource = client.target("http://example.com/webapi"); 
....
我正在使用:

Websphere Liberty profile 16.0.0.2, 
jaxrs-2.0 [1.0.0] 
jaxrsClient-2.0 [1.0.0]
IDE : RDz

你快到了。您只需将请求数据格式化为“实体”实例,并将其发送到您的服务

下面是为一个非常简单的JAX-RS服务执行此操作的一些示例代码

    import javax.ws.rs.client.Client;
    import javax.ws.rs.client.ClientBuilder;
    import javax.ws.rs.client.Entity;
    import javax.ws.rs.client.WebTarget;
    import javax.ws.rs.core.MediaType;

    ....

    // Set up our client and target our JAX-RS service
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://localhost:9081/example.jaxrs/test/SimpleService");

    // Build our request JSON into an 'Entity'. Replace 'myData' with your JSON
    Entity<String> data = Entity.entity("MyData", MediaType.APPLICATION_JSON_TYPE);

    // Then send a post request to the target service
    String result = target.request(MediaType.APPLICATION_JSON_TYPE).post(data, String.class);
导入javax.ws.rs.client.client;
导入javax.ws.rs.client.ClientBuilder;
导入javax.ws.rs.client.Entity;
导入javax.ws.rs.client.WebTarget;
导入javax.ws.rs.core.MediaType;
....
//设置我们的客户机并以JAX-RS服务为目标
Client Client=ClientBuilder.newClient();
WebTarget=client.target(“http://localhost:9081/example.jaxrs/test/SimpleService");
//将我们的请求JSON构建成一个“实体”。用JSON替换“myData”
实体数据=Entity.Entity(“MyData”,MediaType.APPLICATION\u JSON\u TYPE);
//然后向目标服务发送post请求
String result=target.request(MediaType.APPLICATION\u JSON\u TYPE).post(data,String.class);
尝试以下操作

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

Client client = ClientBuilder.newClient();
WebTarget myResource = client.target("http://example.com/webapi"); 
Invocation.Builder invocationBuilder = myResource.request(MediaType.TEXT_PLAIN_TYPE);
Response getResponse = invocationBuilder.get();
if (getResponse != null && getResponse.getStatus() == 200) {
   String responseString = getResponse.readEntity(String.class);
}

您好,Michael,谢谢您的回答,但我仍然有警告和错误:[警告]HTTP响应代码似乎已损坏[WARNING]用于{}WebClient的侦听器引发了异常,现在展开无法发送消息。