I have written simple REST web service client class which uses the JAX-RS 2.0 client API to make REST requests. I am trying to figure out how to set a request timeout for each invocation. Here is the code for a request:
Client client = ClientBuilder.newBuilder().build();
WebTarget resourceTarget = client.target(restServiceUrl)
.path("{regsysID}/{appointmentID}/")
.resolveTemplate("regsysID", regSysId)
.resolveTemplate("appointmentID", apptId);
Invocation invocation = resourceTarget.request(MediaType.APPLICATION_JSON).buildPut(null);
String createSessionJson = invocation.invoke(String.class);
Note: this is a new method available on JAX-RS 2.1
This is a very old post but the below code will work for both jersey and resteasy.
ClientBuilder clientBuilder = ClientBuilder.newBuilder();
clientBuilder.connectTimeout(10, TimeUnit.SECONDS);
clientBuilder.readTimeout(12, TimeUnit.SECONDS);
Client client = clientBuilder.build();
You can do this by creating a ClientConfig first and providing it as an argument when creating the new client.
import org.glassfish.jersey.client.ClientProperties;
ClientConfig configuration = new ClientConfig();
configuration.property(ClientProperties.CONNECT_TIMEOUT, 1000);
configuration.property(ClientProperties.READ_TIMEOUT, 1000);
Client client = ClientBuilder.newClient(configuration);
With Resteasy this can be accomplished by building your Client as such.
Client client = new ResteasyClientBuilder()
.establishConnectionTimeout(2, TimeUnit.SECONDS)
.socketTimeout(2, TimeUnit.SECONDS)
.build();
I have not seen a list of standard configuration properties you could set via ClientBuilder.newClient(Configuration configuration) which would be needed to make this portable.
First, you have to add relevant dependencies (here is for the WildFly 10.1):
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.14.Final</version>
<scope>provided</scope>
</dependency>
Next - create a normal Apache HttpClient and push it the RestEasy Enginge with overriding one method, which causes the problem:
// create here a normal Apache HttpClient with all parameters, that you need
HttpClient httpClient = createHttpClient(connectTimeout,
socketTimeout,
connectionRequestTimeout,
maxTotalHTTPConnections);
// Deprecated Apache classes cleanup https://issues.jboss.org/browse/RESTEASY-1357
// Client Framework not honoring connection timeouts Apache Client 4.3 https://issues.jboss.org/browse/RESTEASY-975
ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient) {
#Override
protected void loadHttpMethod(ClientInvocation request, HttpRequestBase httpMethod) throws Exception {
super.loadHttpMethod(request, httpMethod);
httpMethod.setParams(new BasicHttpParams());
}
};
return new ResteasyClientBuilder().httpEngine(engine).build();
Have a look at https://issues.jboss.org/browse/RESTEASY-975 Seems, that the problem was just resolved in the version 3.1.0.Final.
For people stuck with older JAX-RS 2.0 API and old Resteasy implementation, you may use this method:
Client client = new ResteasyClientBuilder()
.establishConnectionTimeout(3, TimeUnit.SECONDS)
.socketTimeout(5, TimeUnit.SECONDS).build();
Despite the name, socketTimeout stands for "read timeout", since by the docs, it stands for "The timeout for waiting for data".
If you are using Jersey 2.x Here it is the simple solution it's work for me
import com.jclient.JClient;
Client c = Client.create();
WebResource webResource = c.resource("requestUrl");
c.setConnectTimeout(yourMins*60*1000);
Related
I'm working on an application where I have to consume a SOAP webservice and convert it to Rest based webservices. I followed the tutorial from Spring team and was capable to generate pojos, but when I try to make the call using the webServiceTemplate I have an error that the host is not recognized which is basically because I'm behind a proxy in our company.
The technology stack I'm using is Spring boot with web module and spring-ws-core, and I would like to know how to set up my proxy data in the webServiceTemplate.
Thanks
Try to setup the template according this answer.
Afterwards you should be able to set it within your class extending WebServiceGatewaySupport using
setWebServiceTemplate(WebServiceTemplate webServiceTemplate)
After a lot of research, I came up with a programmatic solution. Once you defined your SOAP client that will extend WebServiceGatewaySupport class, I created a configuration class (annotated with #configuration that will declare a bean of my SOAP client. In this method, I used the following code to setup my proxy information and thus I was able to consume my web service:
#Bean
public CommerceSoapClient commerceSoapClient(Jaxb2Marshaller marshaller) {
CommerceSoapClient commerceService = new CommerceSoapClient();
//Setup proxy
HttpClientBuilder builder = HttpClientBuilder.create();
builder.addInterceptorFirst(new HttpComponentsMessageSender.RemoveSoapHeadersInterceptor());
HttpHost proxy = new HttpHost("127.0.0.1", 8080);
builder.setProxy(proxy);
CloseableHttpClient httpClient = builder.build();
HttpComponentsMessageSender messageSender = new HttpComponentsMessageSender(httpClient);
WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
webServiceTemplate.setMessageSender(messageSender);
webServiceTemplate.setDefaultUri("http://your webservice address path");
webServiceTemplate.setUnmarshaller(marshaller);
webServiceTemplate.setMarshaller(marshaller);
commerceService.setDefaultUri("https://your webservice address path");
commerceService.setWebServiceTemplate(webServiceTemplate);
return commerceService;
}
I am trying to access sample Rest method using Webtarget code in Websphere Liberty profile deployed as war and getting following exception.
[WARNING ] Interceptor for {https://www.google.com}WebClient has thrown exception, unwinding now
Could not send Message.
Its working when directly run with java main method.
#GET
#Produces("text/plain")
#Path("/hello")
public Response healthCheck() {
ClientConfig configuration = new ClientConfig();
configuration = configuration.property(ClientProperties.CONNECT_TIMEOUT, 30000);
configuration = configuration.property(ClientProperties.READ_TIMEOUT, 30000);
configuration = configuration.property(ClientProperties.PROXY_URI, "http://xxx.xxx.com:8080");
configuration.connectorProvider(new ApacheConnectorProvider());
Client client = ClientBuilder.newClient(configuration);
WebTarget target = client.target(
"https://www.google.com");
String content = target.request().get(String.class);
System.out.println(content);
}
Any help is appreciated? Its simple task but taking lot of time.
The ClientConfig and ClientProperties types are specific to Jersey. While you might have them in your application, they will almost certain conflict with WebSphere's JAX-RS implementation based on CXF. If you post the full logs, I may be able to confirm that.
Try using the JAX-RS spec API types instead of the Jersey types - and use the IBM properties (unfortunately, these properties are not portable) like this:
#GET
#Produces("text/plain")
#Path("/hello")
public Response healthCheck() {
Client client = ClientBuilder.newBuilder()
.property("com.ibm.ws.jaxrs.client.connection.timeout", 30000)
.property("com.ibm.ws.jaxrs.client.receive.timeout", 30000)
.property("com.ibm.ws.jaxrs.client.proxy.host", "xxx.xxx.com")
.property("com.ibm.ws.jaxrs.client.proxy.port", "8080")
.build();
WebTarget target = client.target(
"https://www.google.com");
String content = target.request().get(String.class);
System.out.println(content);
return Response.ok(content).build();
}
Hope this helps, Andy
Working on a JAX-RS client using CXF.
Requirement is to test the service side code from unit test case with the help of a test client without deploying the rest service to a server
REST Service we implemented use HTTP headers to pass some information to the WS methods.
All the implementations I came across, so far, have services deployed to a server.
Examples I tried to create a JAX-RS client
WebClient client = WebClient.create(ENDPOINT_ADDRESS);
and
RestWSInterafce proxy = JAXRSClientFactory.create(ENDPOINT_ADDRESS, RestWSInterafce.class);
Client client = WebClient.client(proxy);
WebClient httpClient = WebClient.fromClient(client);
I tried configuring a CXF-Jetty implementation but didn't had any luck. Below is a snapshot from the Spring application context file
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-http-jetty.xml" />
<jaxrs:client id="restWSClient" address="${server.address}" serviceClass="com.xyz.rs.RestWSInterface">
</jaxrs:client>
Is it possible to test without having the rest services deployed to a server?
Edit
My main concern while posting this question is to test the method which expects HttpHeaders elements to be passed in the context. Consider the below GET method from my interface.
#GET
#Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
#Path("/getMessage")
public Response getMessage(#Context HttpHeaders headers);
To Unit test this method, instead of calling the interface by deploying the application to the server, I wrote test case to call the implementation class directly and pass an implementor to HttpHeader as input. This solved my issue of unit testing the ws methods without deploying to the server.
public class HttpHeadersImpl implements HttpHeaders {
private MultivaluedMap<String, String> multivaluedMap;
public void setRequestHeaders(MultivaluedMap<String, String> multivaluedMap) {
this.multivaluedMap = multivaluedMap;
}
#Override
public MultivaluedMap<String, String> getRequestHeaders() {
return multivaluedMap;
}
}
You can launch programmaticaly a CXF server from your unit tests without deploying into a server
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
sf.setResourceClasses(TestServiceImpl.class);
sf.setAddress("http://localhost:8080/testrs");
sf.create();
To start the endpoint you will need the cxf-rt-transports-http-jetty dependency
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.1.6</version>
</dependency>
The server could be the real or simulated only for testing. With the WebClient you can test the http transport.
If you work with CXF and Spring, you also can initialize the full context using ClassPathXmlApplicationContext and inject the rest server to the unit test client. In this case you can test the services without http transport
I have a SOAP webservice endpoint url(let's say :"EP1") which we can connect only through a proxy server.
We are using org.springframework.ws.client.core.WebServiceTemplate class to consume webservices.
My question is, Is there a way that I can pass/set the proxy server details on WebServiceTemplate to connect to endpoint url "EP1"?
You can specify custom HttpClient via HttpComponentsMessageSender when constructing the WebServiceTemplate. You can then pass default request config which includes the proxy to the client. Something like this should work:
RequestConfig config = RequestConfig
.custom()
.setProxy(new HttpHost("host"))
.build();
CloseableHttpClient client = HttpClients
.custom()
.setDefaultRequestConfig(config)
.build();
HttpComponentsMessageSender messageSender = new HttpComponentsMessageSender(client);
WebServiceTemplate wsTemplate = new WebServiceTemplate();
wsTemplate.setMessageSender(messageSender);
// Set other required properties ...
You might need to set other properties on the WebServiceTemplate object or the HttpClient depending on your needs so on. But this should demonstrate the basic concept.
Also take a look at this sample illustrating the usage of proxies in Apache HTTP client.
You must use VM arguments: -Dhttp.proxyHost=mydomain.com -Dhttp.proxyPort=8080
Having in mind that this setting applies to all HTTP requests made from Java.
I have a REST controller that has multiple GET/POST/PUT methods that all respond/request JSON.
I am not using Spring in this application (yet).
I was looking into the REST-assured framework and I like how that looks but I can only use it when my web server is up and running.
Is there a way for me to run a in-memory web server, or something like that?
Are there any examples of REST endpoint testing that someone can provide?
If you are using JAX-RS 2.0 you should find your answer here
You can take a look at the example also
An integration test example, could be:
public class CustomerRestServiceIT {
#Test
public void shouldCheckURIs() throws IOException {
URI uri = UriBuilder.fromUri("http://localhost/").port(8282).build();
// Create an HTTP server listening at port 8282
HttpServer server = HttpServer.create(new InetSocketAddress(uri.getPort()), 0);
// Create a handler wrapping the JAX-RS application
HttpHandler handler = RuntimeDelegate.getInstance().createEndpoint(new ApplicationConfig(), HttpHandler.class);
// Map JAX-RS handler to the server root
server.createContext(uri.getPath(), handler);
// Start the server
server.start();
Client client = ClientFactory.newClient();
// Valid URIs
assertEquals(200, client.target("http://localhost:8282/customer/agoncal").request().get().getStatus());
assertEquals(200, client.target("http://localhost:8282/customer/1234").request().get().getStatus());
assertEquals(200, client.target("http://localhost:8282/customer?zip=75012").request().get().getStatus());
assertEquals(200, client.target("http://localhost:8282/customer/search;firstname=John;surname=Smith").request().get().getStatus());
// Invalid URIs
assertEquals(404, client.target("http://localhost:8282/customer/AGONCAL").request().get().getStatus());
assertEquals(404, client.target("http://localhost:8282/customer/dummy/1234").request().get().getStatus());
// Stop HTTP server
server.stop(0);
}
}