6.1 Practical part: Creation of X-Road data service and client based on WSDL (on Java platform)

In this chapter, we create a new X-Road data service based on a previously created WSDL description of an X-Road data service and test the X-Road service with various means.

For the creation of an X-Road data service, we use the following tools: Apache CXF 3.2, Maven.

Used method: ‘Top Down’ – i.e., based on the available WSDL, we generate a Java code which can be later changed manually as necessary.

6.1.1        Creation of X-Road data service

For creating an X-Road data service, we use Eclipse’s Maven, which generates a necessary code via CXF. For this, we change the pom.xml file. Adding necessary <properties>, <dependency> and <plugin> components.

First off, add <properties> and <dependency> so that Maven would download all the necessary jar files. If necessary, all files can be downloaded manually.

<!-- properties -->

<properties>

<cxf.version>3.2.0</cxf.version>

<spring.version>4.3.11.RELEASE</spring.version>

<cxf.scope>compile</cxf.scope>

<compileSource>1.8</compileSource>

<maven.compiler.target>1.8</maven.compiler.target>

<maven.compiler.source>1.8</maven.compiler.source>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

<dependencies>

<!-- apache cxf jax-ws 3.2.0 -->

<dependency>

<groupId>org.apache.cxf</groupId>

<artifactId>cxf-core</artifactId>

<version>${cxf.version}</version>

</dependency>

<dependency>

<groupId>org.apache.cxf</groupId>

<artifactId>cxf-rt-frontend-jaxws</artifactId>

<version>${cxf.version}</version>

<scope>${cxf.scope}</scope>

</dependency>

<dependency>

<groupId>org.apache.cxf</groupId>

<artifactId>cxf-rt-transports-http</artifactId>

<version>${cxf.version}</version>

<scope>${cxf.scope}</scope>

</dependency>

<!-- spring framework 4.3.11 -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>${spring.version}</version>

<scope>compile</scope>

</dependency>

</dependencies>


After this, select Run As > Maven install on the project so that Maven would download all the necessary Jar files.

Add <plugin>, which allows for the files to be generated. For this, you have to change:


  • <sourceRoot>: catalogue where the Java files are generated. By default, it is "target/generated-sources/cxf”. Set it as, for example: “${basedir}/src/main/java“

  • <wsdl>: WSDL from which files are generated

  • <wsdlLocation>: Location where @WebService will be referring to

    <build>

    <finalName>persons_register</finalName>

    <plugins>

    <!-- plugin 4- apache cxf codegen wsdl2java goal -->

    <plugin>

    <groupId>org.apache.cxf</groupId>

    <artifactId>cxf-codegen-plugin</artifactId>

    <version>${cxf.version}</version>

    <executions><execution>

    <configuration>

    <sourceRoot>${basedir}/src/main/java</sourceRoot>

    <wsdlOptions><wsdlOption>

    <wsdl>${basedir}/src/main/resources/persons_register.wsdl</wsdl>

    <wsdlLocation>classpath:persons_register.wsdl</wsdlLocation>

     <extraargs>

    <extraarg>-impl</extraarg>

    <extraarg>-exsh</extraarg>

    <extraarg>true</extraarg>

    </extraargs>

    </wsdlOption>

    </wsdlOptions>

    </configuration>

    <goals><goal>wsdl2java</goal></goals>

    </execution></executions></plugin></plugins></build>

     

    For generating, select on the project Run As > Maven generate-sources.

    Additional information about pom.xml: https://maven.apache.org/guides/introduction/introduction-to-the-pom.html

    You can additionally read: http://cxf.apache.org/docs/maven-cxf-codegen-plugin-wsdl-to-java.html


<extraargs> affect how the code is generated. You can read about it: http://cxf.apache.org/docs/wsdl-to-java.html


6.1.2        Configuration of X-Road data service

For the data service to be available, you have to change src > main > webapp > WEB-INF > web.xml.

<param-value> beans file name, where you can determine the service endpoint.

 

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<display-name>persons_register</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<servlet>

<description>Apache CXF Endpoint</description>

<display-name>cxf</display-name>

<servlet-name>cxf</servlet-name>

<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>cxf</servlet-name<url-pattern>/services/*</url-pattern>

</servlet-mapping>

<session-config><session-timeout>60</session-timeout></session-config>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>WEB-INF/cxf-beans.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

</web-app>

 

Set up <jaxws:endpoint> according to your data service.

  • id: the name of beans. It is not mandatory and can be skipped.

  • implementor: Java implementation of the service

  • wsdlLocation: WSDL file

  • address: service address

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      xmlns:jaxws="http://cxf.apache.org/jaxws"

      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:endpoint id="personregisterSOAP"  implementor="ee.x_road.persons_register.Person_RegisterSOAPImpl"

        wsdlLocation="classpath:persons_register.wsdl"

        address="/PersonRegisterSOAP">

    </jaxws:endpoint>

</beans>

  

Check the generated data service

A new data service has been generated. To make sure, check if:

    • the source code of the service has been generated into the src folder;

    • a new java package has been created, for example: ee.x_road.persons_register (the name depends on WSDL);

    • check the data service endpoint and the WSDL:

      The generated service has been activated and it responds on the address:

      http://localhost:8080/person_register/services 

      (NB! here, instead of person_register, everyone has their own Eclipse project name)

       

      To check the data service, enter said address into the browser, after which information will be displayed about the created data service:

  • the address of the data service endpoint

  • data service WSDL address (link to WSDL)


Check if the WSDL address of the data service will be displayed by clicking on the WSDL link, for example: http://localhost:8080/person_register/services/person_registerSOAP?wsdl

(The exact address of the link depends on the WSDL used)

We will use this WSDL address later when setting up the service on the X-Road security server and when testing it via SoapUI.

Request response data

Generated X-Road data service guarantees constant generated data in the request response. In real life, generated data has to be removed from the code, and for obtaining data, database request has to be used.

When testing, you can use data generated in the response or change it manually.

6.1.3        Creation of X-Road Java client application

Creation of Java client application project is the same as the creation of service (see module 5). WSDL should be copied from the service into the client application because with both, the same WSDL file has to be used.

 

The <properties> and <dependency> in the pom.xml file are also identical to the ones added during creation of service (see “Creation of X-Road data service“ in module 6). In the <plugin> tag, <extraarg>-client</extraarg> parameter should be used instead of <extraarg>-impl</extraarg> .

Web.xml does not need to be changed, nor does beans.xml need to be created.

 

You need to add endpoint in the generated file of the client:

            ((javax.xml.ws.BindingProvider) port).getRequestContext().put(

            javax.xml.ws.BindingProvider.ENDPOINT_ADDRESS_PROPERTY,

            "http://localhost:8080/person_register/services/PersonRegisterSOAP");

 

Java code of the client of the data service has been generated, now check if:

  • the generated code is inside the src folder

  • the service’s exemplary call is implemented in the <PersonRegister_PersonRegister>SOAP_Client.java file’s main() method, where instead of PersonRegister, there is a name depending on the WSDL of a specific data service.

  • Initialised data objects with generated data have been created for calling the service.

Activation of service’s client program:

  • Select file *SOAP_Client.java

  • Select from the menu: Run as / Java Application

  • Check the program’s output in the console

 

Java client testing via X-Road security server:

  • change the service endpoint in the*SOAP_Client.java file, so that it would refer to the security server:

    // X-road : change service endpoint address to security server address

            ((javax.xml.ws.BindingProvider)port).getRequestContext().

            put(javax.xml.ws.BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http:// 10.239.255.100" );

  • Check if it is correct in the generated code:

    eu.x_road.xsd.identifiers.XRoadObjectType _personList_serviceObjectType =;

    eu.x_road.xsd.identifiers.XRoadObjectType.SERVICE;

    eu.x_road.xsd.identifiers.XRoadObjectType _personList_clientObjectType = eu.x_road.xsd.identifiers.XRoadObjectType.SUBSYSTEM;

     

  • Adjust the code generated by *SOAP_Client.java by correcting the X-Road header fields that contain generated data (like during previous testing with the SoapUI program)

(If needed, you can look at an example in the sample file PersonRegister_PersonRegisterSOAP_Client.java)






Viimati muudetud: kolmapäev, 25. oktoober 2017, 16:03