Narzędzie wsdl2java
Narzędzie wsdl2java
Artykuł ten ma na celu pokazać w jaki sposób wywoływać usługi sieciowe z użyciem protokołu SOAP, Springa i Mavena. W artykule https://javaleader.pl/2019/12/20/apache-servicemix-wprowadzenie/ opisałem w jaki sposób wystawić usługi sieciowe opisane przez plik *.wsdl (z ang .Web Services Description Language). Projekt zainstalowany został z użyciem szyny ESB – Apache Service Mix. Na podstawie wygenerowanego pliku WSDL z użyciem narzędzia SoapUI z powodzeniem wywołaliśmy zaprojektowane wcześniej usługi sieciowe. W tym wpisie zobaczysz jak na podstawie dostępnego pliku WSDL wygenerować odpowiednie klasy Javy które pozwolą na wykonanie dostępnych usług sieciowych. Do tego celu użyjemy narzędzia wsdl2java. W pierwszej kolejności zainstalujmy projekt z wspomnianego wpisu – https://javaleader.pl/2019/12/20/apache-servicemix-wprowadzenie/. Zaczynamy teraz od nowego projektu Apache Maven – plik pom.xml – niezbędne zależności:
<dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-common-utilities</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-tools-common</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-simple</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- add spring support --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.1.RELEASE</version> </dependency> <!-- add spring support --> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <verbose>true</verbose> <fork>true</fork> <compilerVersion>${java.version}</compilerVersion> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <!-- cxf codegen plugin for WSDL to Java --> <plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin</artifactId> <version>${cxf.version}</version> <executions> <execution> <id>stock-quote-service</id> <phase>generate-sources</phase> <configuration> <sourceRoot>${project.basedir}/src/main/java/generated_java</sourceRoot> <wsdlOptions> <wsdlOption> <wsdl>${project.basedir}/src/main/resources/META-INF/wsdl/calculator.wsdl</wsdl> <wsdlLocation>http://localhost:8181/cxf/calcService?wsdl</wsdlLocation> <serviceName>CalculatorImplService</serviceName> <extraargs> <extraarg>-verbose</extraarg> <extraarg>-p</extraarg> <extraarg>pl.javaleader.ws</extraarg> </extraargs> </wsdlOption> </wsdlOptions> </configuration> <goals> <goal>wsdl2java</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-xjc-ts</artifactId> <version>2.2.9</version> </dependency> </dependencies> </plugin> </plugins> </build>
Wykonujemy komendę:
mvn generate-sources
wynik:
Loading FrontEnd jaxws ...
Loading DataBinding jaxb ...
jeśli zależy nam na ponownym wygenerowaniu niezbędnych klas należy najpierw wyczyścić projekt poleceniem:
mvn clean
Klasa testująca:
public class Demo { public static void main(String args[]) { CalculatorImplService service = new CalculatorImplService(); Calculator calculatorProxy = service.getCalculatorImplPort(); int resultAddition = calculatorProxy.addidtion(10, 20); System.out.println("Sum of 10+20 = " + resultAddition); int resultSubtraction = calculatorProxy.subtraction(10, 20); System.out.println("Subtraction of 10-20 = " + resultSubtraction); } }
wynik – z powodzeniem udało się wykonać dostępne usługi sieciowe:
mar 06, 2020 9:53:02 AM org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL INFO: Creating Service {http://ws.javaleader.pl/}CalculatorImplService from WSDL: http://localhost:8181/cxf/calcService?wsdl Sum of 10+20 = 30 Subtraction of 10-20 = -10
Dodajmy teraz plik client-beans.xml w katalogu ./resources w którym to zarejestrujemy beana calcClient:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <jaxws:client id="calcClient" serviceClass="pl.javaleader.ws.Calculator" address="http://localhost:8181/cxf/calcService" /> </beans>
oraz następujący fragment kodu do klasy Demo który na podstawie zarejestrowanego beana calcClient wykona dostępne usługi sieciowe:
public class Demo { public static void main(String args[]) { ApplicationContext context = new ClassPathXmlApplicationContext("/client-beans.xml"); Calculator soap = (Calculator) context.getBean("calcClient"); System.out.println(soap.addidtion(2,3)); System.out.println(soap.subtraction(10,3)); } }
wynik:
mar 06, 2020 9:59:26 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@443b7951: startup date [Fri Mar 06 09:59:26 CET 2020]; root of context hierarchy mar 06, 2020 9:59:26 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [client-beans.xml] mar 06, 2020 9:59:26 AM org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass INFO: Creating Service {http://ws.javaleader.pl/}CalculatorService from class pl.javaleader.ws.Calculator 5 7
Leave a comment