0% found this document useful (0 votes)
2 views

Java Web Services Client

Java notes lecturer

Uploaded by

Francis Martin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Web Services Client

Java notes lecturer

Uploaded by

Francis Martin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

JAVA WEB SERVICES CLIENT

Unit 5

Java Web Services Client

1. Understanding Web Services

Web services enable communication between different applications over the


internet using standard protocols such as HTTP, XML, SOAP, or REST. There are
two main types:

 SOAP-based Web Services: Use XML-based SOAP protocol for exchanging


structured data.
 RESTful Web Services: Use HTTP methods (GET, POST, PUT, DELETE)
for operations and often exchange data in JSON or XML.

2. Tools and Frameworks

To write a Java Web Services Client, you can use several tools and libraries:

 JAX-WS (Java API for XML Web Services): For SOAP-based services.
 JAX-RS (Java API for RESTful Web Services): For RESTful services.
 Apache CXF: Framework for SOAP and RESTful services.
 Jersey: Popular JAX-RS implementation.
 HTTPClient or OkHttp: Libraries for custom HTTP requests.
 Spring WebClient: Part of Spring Framework for calling web services.

3. Writing a Client for SOAP-based Web Services

Step 1: Generate the Client Code

1. Obtain the WSDL (Web Services Description Language) file of the SOAP
web service.
2. Use tools like wsimport (JDK-provided) to generate client-side stubs.

Obtain the WSDL (Web Services Description Language) file of the SOAP web
service.

Use tools like wsimport (JDK-provided) to generate client-side stubs.

wsimport -keep -p <package_name> <wsdl_url>


JAVA WEB SERVICES CLIENT
Unit 5

-keep: Retains generated Java source files.

-p: Specifies the package name for the generated classes.

Step 2: Set Up the Project

Add required dependencies to your pom.xml (if using Maven):

<dependency>

<groupId>javax.xml.ws</groupId>

<artifactId>jaxws-api</artifactId>

<version>2.3.1</version>

</dependency>

Step 3: Write the Client Code

Use the generated stubs to call the service.

import com.example.ws.Service;

import com.example.ws.ServicePortType;

public class SoapClient {

public static void main(String[] args) {


JAVA WEB SERVICES CLIENT
Unit 5

Service service = new Service();

ServicePortType port = service.getServicePort();

String response = port.yourMethod("input");

System.out.println("Response: " + response);

4. Writing a Client for RESTful Web Services

Option 1: Using JAX-RS (Jersey)

Add Jersey dependency in pom.xml:

<dependency>

<groupId>org.glassfish.jersey.core</groupId>

<artifactId>jersey-client</artifactId>

<version>3.0.2</version>

</dependency>

Write the client code:

import jakarta.ws.rs.client.Client;

import jakarta.ws.rs.client.ClientBuilder;
JAVA WEB SERVICES CLIENT
Unit 5

import jakarta.ws.rs.client.WebTarget;

import jakarta.ws.rs.core.MediaType;

import jakarta.ws.rs.core.Response;

public class RestClient {

public static void main(String[] args) {

Client client = ClientBuilder.newClient();

WebTarget target = client.target("http://example.com/api/resource");

Response response =
target.request(MediaType.APPLICATION_JSON).get();

String responseBody = response.readEntity(String.class);

System.out.println("Response: " + responseBody);

response.close();

Option 2: Using Spring WebClient

Add Spring WebFlux dependency:

<dependency>

<groupId>org.springframework.boot</groupId>
JAVA WEB SERVICES CLIENT
Unit 5

<artifactId>spring-boot-starter-webflux</artifactId>

</dependency>

Write the client code:

import org.springframework.web.reactive.function.client.WebClient;

public class RestClient {

public static void main(String[] args) {

WebClient client =
WebClient.create("http://example.com/api/resource");

String response = client.get()

.retrieve()

.bodyToMono(String.class)

.block();

System.out.println("Response: " + response);

5. Authentication

If the web service requires authentication:


JAVA WEB SERVICES CLIENT
Unit 5

Basic Authentication: Include credentials in the HTTP header.

client.register(HttpAuthenticationFeature.basic("username", "password"));

OAuth2: Use access tokens.

Custom Tokens: Add them to the request headers.

6. Exception Handling

Always handle exceptions to manage network issues or invalid responses.

try {

// REST/SOAP call

} catch (Exception e) {

System.err.println("Error: " + e.getMessage());

7. Debugging

Enable logging to see HTTP requests and responses for debugging.

For SOAP: Enable SOAPMessage logging.


JAVA WEB SERVICES CLIENT
Unit 5

System.setProperty("com.sun.xml.ws.transport.http.client.HttpTransportPipe
.dump", "true");

System.setProperty("com.sun.xml.internal.ws.transport.http.client.HttpTran
sportPipe.dump", "true");

For REST: Use libraries like SLF4J or Logback.

8. Testing the Client

Use tools like Postman or cURL to verify the web service independently.

Write unit tests for the client using frameworks like JUnit and mock the
service with WireMock or MockServer.

By following these steps, you can create a robust Java web services client for
both SOAP and RESTful APIs.

You might also like