Problem
If I would like to consume a web service which provide by http://localhost:8080/ws/eaWs.wsdl
How to create SOAP client to consume web services?
How-To
1. Generate Java classes via SoapUI and save WSDL file into resources/wsdl
2. Create WsClientConfig class:
package com.example.soapClient.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.ws.client.core.WebServiceTemplate;
@Configuration
public class WsClientConfig {
private String defaultUri = "http://localhost:8080/ws/eaWs";
@Bean
public Jaxb2Marshaller jaxb2Marshaller() {
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
jaxb2Marshaller.setContextPaths("ws.oi2.npa.ea.country", "ws.oi2.npa.ea.emp");
return jaxb2Marshaller;
}
@Bean
public WebServiceTemplate webServiceTemplate() {
WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
webServiceTemplate.setMarshaller(jaxb2Marshaller());
webServiceTemplate.setUnmarshaller(jaxb2Marshaller());
webServiceTemplate.setDefaultUri(defaultUri);
return webServiceTemplate;
}
}
3. Create Country Client Class
package com.example.soapClient.client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ws.client.core.WebServiceTemplate;
import ws.oi2.npa.ea.country.Country;
import ws.oi2.npa.ea.country.GetCountryRequest;
import ws.oi2.npa.ea.country.GetCountryResponse;
@Component
public class CountryClient {
@Autowired
private WebServiceTemplate webServiceTemplate;
public void getCountry(String name) {
GetCountryRequest request = new GetCountryRequest();
request.setName(name);
GetCountryResponse response = (GetCountryResponse) webServiceTemplate.marshalSendAndReceive(request);
Country country = response.getCountry();
System.out.println("[getCountry] name = " + country.getName() + ", capital = " + country.getCapital());
}
}
4. Create Employee Client Class
package com.example.soapClient.client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ws.client.core.WebServiceTemplate;
import ws.oi2.npa.ea.emp.Employee;
import ws.oi2.npa.ea.emp.GetEmployeeByIdRequest;
import ws.oi2.npa.ea.emp.GetEmployeeRequest;
import ws.oi2.npa.ea.emp.GetEmployeeResponse;
@Component
public class EmployeeClient {
@Autowired
private WebServiceTemplate webServiceTemplate;
public void getEmployeeByName(String name) {
GetEmployeeRequest request = new GetEmployeeRequest();
request.setName(name);
GetEmployeeResponse response = (GetEmployeeResponse) webServiceTemplate.marshalSendAndReceive(request);
Employee employee = response.getEmployee();
System.out.println("[getEmployeeByName] id = " + employee.getId() + ", name = " + employee.getName());
}
public void getEmployeeById(Integer id) {
GetEmployeeByIdRequest request = new GetEmployeeByIdRequest();
request.setId(id);
GetEmployeeResponse response = (GetEmployeeResponse) webServiceTemplate.marshalSendAndReceive(request);
Employee employee = response.getEmployee();
System.out.println("[getEmployeeById] id = " + employee.getId() + ", name = " + employee.getName());
}
}
5. Do test
package com.example.soapClient;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import com.example.soapClient.client.CountryClient;
import com.example.soapClient.client.EmployeeClient;
@SpringBootApplication
public class SoapClientApplication {
public static void main(String[] args) {
SpringApplication.run(SoapClientApplication.class, args);
}
@Bean
CommandLineRunner test(CountryClient cClient, EmployeeClient eClient) {
return args -> {
cClient.getCountry("台灣");
eClient.getEmployeeById(1);
eClient.getEmployeeByName("Mandy");
};
}
}
Test result:
[getCountry] name = 台灣, capital = 台北
[getEmployeeById] id = 1, name = Albert
[getEmployeeByName] id = 2, name = Mandy
Reference
[1] https://spring.io/guides/gs/consuming-web-service/