Sie sind auf Seite 1von 15

Middleware – Web Service Concat – GSOAP -

MAVEN

2 avril 2019

Le service WSConcat
ava web services are developed through the use of servlets that embed a web
J service stack. Such servlets are executed in a servlet container. Managing
a project related to such web services is a daunting tasks. Thus, maven will be
used to ease the development and management of the project. The aim of this
work is to setup a such project and to study how it works.
1. Enter the following command line to create a new maven project called WSCon-
catService
# mvn archetype:generate -DgroupId=net.tuto2.ws.service.concat
-DartifactId=WSConcatService -DarchetypeArtifactId=maven-
archetype-webapp

Base revision 9e79146, Wed Feb 18 18:23:17 2015 +0100, David Bromberg.
2

2. You should get the following tree directory in your file lsystem :
WSConcatService
pom.xml
src
main
java
resources
webapp
WEB-INF
target
à pom.xml, this file describes in details your current project
à src, contains all the source file of your project
à main/java, contains particularly all your Java source files that will be
included in the generated JAR, WAR packages, etc. . .
à main/webapp, contains .jsp pages
à main/WEB-INF, contains servlet configuration files
à target, in this directory you will find code that has been generated from
source files, etc. . .

Some of your directory may not be created as for example src/main/java.


In this case, you should create them by hand.

3. Edit the pom.xml which is at the root of your project directory (under the
WSConcatService directory). Add the following directories to enable maven to
download the required libraries to compile your project.

Listing 1 – pom.xml ü
Line 1 <repositories>
- <repository>
- <id>maven2-repository.dev.java.net</id>
- <name>Java.net Repository for Maven</name>
5 <url>https://maven.java.net/content/repositories/
releases</url>
- </repository>
- </repositories>

à It must have only one xml node <respositories> in a pom.xml file

David Bromberg Base revision 9e79146 www.david.bromberg.fr


3

4. If you have any issues with the remote repositories, you can add the additional
repository.

Listing 2 – pom.xml ü
Line 1 <repositories>
- ...
- <repository>
- <id>maven2-repository.dev.java.net</id>
5 <name>Java.net Repository for Maven 2</name>
- <url>http://download.java.net/maven/2/</url>
- </repository>
- <repository>
- <id>maven2-bromberg.dev.java.net</id>
10 <name>Java.net Repository for Maven 2</name>
- <url>http://www.labri.fr/perso/bromberg/repo/</url>
- </repository>
- ...
- </repositories>

David Bromberg Base revision 9e79146 www.david.bromberg.fr


4

5. Edit the pom.xml file located in the root of your WSConcatService project. You
must specify which compiler version you wish to use to compile your project. In
our specific case, we want to produce 1.5 compliant Java bytecode as we are
going to use annotations in Java source files.

Listing 3 – pom.xml ü
Line 1 <project>
- ...
- <build>
- <plugins>
5 <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.0.2</version>
- <configuration>
10 <source>1.5</source>
- <target>1.5</target>
- </configuration>
- </plugin>
- </plugins>
15 </build>
- ...
- </project>

à It must have exactly one xml node <build> in your pom.xml file
à It must have exactly one xml node<plugins></plugins> included in the
node <build> in your pom.xml file.

David Bromberg Base revision 9e79146 www.david.bromberg.fr


5

6. You must declare the required dependency for your project. For this project, you
are going to use a web service framework provided by Sun. Consequently, you
should add this dependency in your pom.xml file as illustrated in the following :

Listing 4 – pom.xml download


Line 1 <project>
- ...
- <dependencies>
- <dependency>
5 <groupId>org.glassfish.metro</groupId>
- <artifactId>webservices-rt</artifactId>
- <version>2.3</version>
- </dependency>
- </dependencies>
10 ...
- </project>

à It must have exactly one xml node <dependencies></dependencies>


included in your pom.xml file.

7. We are going to create a web service by following the last contract methodology
that consists to avoid to write yourself the WSDL interface of your service. As
writing a WSDL interface is cumbersome it may be easier at the beginning to
generate it instead of writing it yourself. This method enables you, in a first
step, to become more familiar with WSDL and XML documents. So, the interface
of your service will be generated automatically from an annotated Java inter-
face that should represent your web service WSConcatService . To a have a
deeper knowledge of the possible annotations, you can have a look to the official
documentation. http://jax-ws.java.net/nonav/2.1.3/docs/annotations.html

David Bromberg Base revision 9e79146 www.david.bromberg.fr


6

8. Create now a Java interface with a method enabling to concatenate two strings.
This interface must be located inside the package
net.tuto2.ws.service.concat in the src/main/java directory.

Listing 5 – WSConcat.java - Interface du service WSConcat download


Line 1 package net.tuto2.ws.service.concat;
-
- import javax.jws.WebMethod;
- import javax.jws.WebParam;
5 import javax.jws.WebResult;
- import javax.jws.WebService;
- import javax.jws.soap.SOAPBinding;
-
- @WebService(targetNamespace = "http://david.bromberg.fr/
service/concat", name = "WSConcat")
10 @SOAPBinding(style = SOAPBinding.Style.RPC)
- public interface WSConcat {
- @WebMethod
- public String concat(
- @WebParam(name = "str1")
15 String str1,
- @WebParam(name = "str2")
- String str2
- );
- }

9. Explain the meaning of the annotations used.

David Bromberg Base revision 9e79146 www.david.bromberg.fr


7

10. Now, you have to implement your service inside a class called WSConcatImpl
located in the package named net.tuto2.ws.service.concat.

Listing 6 – WSConcatImpl.java - Implémentation du service download


Line 1 package net.tuto2.ws.service.concat;
-
- import javax.jws.WebParam;
- import javax.jws.WebService;
5
- @WebService(endpointInterface = "
net.tuto2.ws.service.concat.WSConcat", serviceName = "
MyConcatAgent")
- public class WSConcatImpl {
-
- public String concat (String str1,String str2){
10 return str1+str2;
- }
- }

David Bromberg Base revision 9e79146 www.david.bromberg.fr


8

11. The next step is to generate the WSDL document corresponding to the WSConcat
Java interface. To this end, it is required to use the wsgen tool available through
the jaxws-maven-plugin. Consequently, you have to update the pom.xml of
your project to add a new step in the project compilation process. The wsgen
tool takes as an input parameter a Service End Interface (sei). It means that it
takes a Java source file from which the WSDL document is generated.

Listing 7 – pom.xmldownload
Line 1 <project>
- ...
- <build>
- ...
5 <plugins>
- ...
- <plugin>
- <groupId>org.jvnet.jax-ws-commons</groupId>
- <version>2.3</version>
10 <artifactId>jaxws-maven-plugin</artifactId>
- <executions>
- <execution>
- <goals>
- <goal>wsgen</goal>
15 </goals>
- <configuration>
- <sei>net.tuto2.ws.service.concat.WSConcatImpl</sei>
- <genWsdl>true</genWsdl>
- <keep>true</keep>
20 </configuration>
- </execution>
- </executions>
- </plugin>
- ...
25 </plugins>
- ...
- </build>
- </project>

12. You can now enter the following command line mvn clean install. Che-
ck/study the generated WSDL documents located into the target/jaxws/ws-
gen/wsdl/ directory. Your service has been created :-)

David Bromberg Base revision 9e79146 www.david.bromberg.fr


Servlet deployment
1. Before being able to deploy your service, you should configure it in order to
make it works inside a servlet container. Go to the following directory
WSConcatService/src/main/webapp/WEB-INF. And edit the web.xml file as fol-
low :

Listing 8 – web.xml ü
Line 1 <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
5 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <display-name>Sample web service provider</display-name>
- <listener>
- <listener-class>com.sun.xml.ws.transport.http.servlet.
WSServletContextListener</listener-class>
- </listener>
10 <servlet>
- <servlet-name>WebServicePort</servlet-name>
- <servlet-class>com.sun.xml.ws.transport.http.servlet.
WSServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
15 <servlet-mapping>
- <servlet-name>WebServicePort</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
- </web-app>
10

2. The web.xml file has been standardized under the JSR-315 specification. The
following XML tags are fundamental for a correct behavior of your web service.
à Tags <servlet>...</servlet>, indicate which servlet should be loaded
by the container. So, in our context, we are using a particular servlet that
embeds a web service stack named Metro from Sun/Oracle. Thus, we are
using com.sun.xml.ws.transport. http.servlet.WSServlet class to
receive all incoming HTTP requests.
à Tags <servlet-mapping>...</servlet-mapping>, enable to specify
which servlet will be executed according to the URL requested by the
HTTP client.
à Tags <url-pattern>...</url-pattern>, are used to define a pattern
that will trigger the incoming request to the adequate servlet. For
instance, <url-pattern>/services/*</url-pattern> will map all re-
quests that include in their URL the pattern /services/ to the servlet
named WebServicePort.
à Tags<listner-class>...</listner-class>, indicate which Java class
that will manage the life cycle of the servlet.

3. Finally, Sun/Oracle servlet uses a particular configuration file named sun-


jaxws.xml to initialize its web service stack. Create this file in the
WSConcatService/src/main/webapp/WEB-INF directory and fill it as recom-
mended below :

Listing 9 – sun-jaxws.xml.xml ü
Line 1 <?xml version="1.0" encoding="UTF-8"?>
- <endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
- version="2.0">
- <endpoint
5 name="test1"
- implementation="net.tuto2.ws.service.concat.WSConcatImpl"
- url-pattern=’/services/concat’/>
- </endpoints>

à Tags <endpoint/>, indicates which web service will answer to invocations


from clients. So, in our context, set the implementation attribute to the
name of your class, the one that implements your service.
à The attribute url-pattern, enables to specify a pattern that triggers the
redirection of incoming requests to adequate to your aforementioned web
service.

http ://www.oracle.com/technetwork/java/javaee/servlet/index.html

David Bromberg Base revision 9e79146 www.david.bromberg.fr


11

4. Update the pom.xml file located at the root of your WSConcatService project,
to specify the servlet container that you wish to use. Currently, we are going to
use a container called Jetty .

Listing 10 – sun-jaxws.xml.xml ü
Line 1 <project>
- ...
- <build>
- <plugins>
5 ...
- <plugin>
- ...
- </plugin>
- <plugin>
10 <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-maven-plugin</artifactId>
- <version>9.0.5.v20130815</version>
- </plugin>
- ...
15 </plugins>
- </build>
- ...
- </project>

5. Once the Jetty plugin has been declared in your pom.xml, you can do
a mvn clean install to download the missing code, i.e, the required plu-
gin. To launch your web service, enter mvn jetty:run in your shell. For
further information on how the plugin works, go to the following URL
http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin. We will re-
member particularly the following key properties :
à The Jetty container is by default accessible from
http://localhost:8080 URL
à Jetty scan periodically the files of your project and if some modifications
has been done on your .class files, your web service is redeployed.
à Jetty is running until it is stopped from the command line by hitting a
<ctrl-c>

6. You can now surf on the web page of your service as illustrated in Figure 1, by
entering the following URL : http://localhost:8080/WSConcatService.

David Bromberg Base revision 9e79146 www.david.bromberg.fr


12

7. To get the WSDL document of your web service freshly deployed, you have
just to enter the following URL in your web browser http://localhost:
8080/WSConcatService/services/concat

By default, the path to access the web service depends of the value given
to the artifactId variable of your project. However, be-careful, according
to version of jetty that is running it is not always the case (the context is
then the root of the URL, i.e. /). Note that it is always possible to indicate
it explicitly in the configuration part of the Jetty plugin. For instance :

Listing 11 – Configuration du plugin Jetty ü


Line 1 <configuration>
- <webApp>
- <contextPath>/${project.artifactId}</contextPath>
- </webApp>
5 <webAppSourceDirectory>${basedir}/src/main/webapp</
webAppSourceDirectory>
- <webXml>${basedir}/src/main/webapp/WEB-INF/web.xml</webXml
>
- </configuration>

Figure 1 – web page of your web service

David Bromberg Base revision 9e79146 www.david.bromberg.fr


13

Build web service clients


t is possible to create clients in either Java or C, it does not matter. This
I is totally transparent for your web service thanks to the underlying web
service middleware that hides the heterogeneity that can occur between the
client and server side.

Building a Java client


1. In order to create a client for your web service named WSConcatService
WSConcatService, create a new Maven project with a groupID equal to
net.tuto2.ws.client, a artifactID equal to WSConcatClient, and a arche-
typeArtifactID equal to maven-archetype-quickstart.

2. The structure of the directory of your project should have the following struc-
ture :
WSConcatClient
pom.xml
src
main
java
resources
target

David Bromberg Base revision 9e79146 www.david.bromberg.fr


14

3. Now we are focused on the client project. To develop the client, we absolu-
tely need to reuse the WSDL document of your web service. The latter should
be available at this url http://localhost:8080/WSConcatService/services/
concat. You must use the tool wsimport to generate the required stubs to in-
voke the remote service. To this end, you have to update the pom.xml file to add
a new step in the life cycle process of your project. More particularly, you have
to add the use of wsimport action in the execution phase as illustrated below :

Listing 12 – pom.xml ü
Line 1 <plugin>
- <groupId>org.jvnet.jax-ws-commons</groupId>
- <artifactId>jaxws-maven-plugin</artifactId>
- <version>2.3</version>
5 <configuration>
- <sourceDestDir>${basedir}/src/main/java</sourceDestDir>
- <wsdlUrls>
- <wsdlUrl>
- http://localhost:8080/WSConcatService/services/concat?wsdl
10 </wsdlUrl>
- </wsdlUrls>
- <packageName>net.tuto2.ws.client</packageName>
- </configuration>
- <executions>
15 <execution>
- <goals>
- <goal>wsimport</goal>
- </goals>
- </execution>
20 </executions>
- </plugin>

4. Test the generation of you WSDL document via the mvn clean install com-
mand.

5. According to the Java generated code, you should be able to code the client
application through the App.java file.

David Bromberg Base revision 9e79146 www.david.bromberg.fr


15

6. In order to execute the client from the command line, you shoud update again
your pom.xml file to add a new step dedicated to the compilation of your client.

Listing 13 – pom.xml ü
Line 1 <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>exec-maven-plugin</artifactId>
- <executions>
5 <execution>
- <goals>
- <goal>exec</goal>
- </goals>
- </execution>
10 </executions>
- <configuration>
- <executable>java</executable>
- <arguments>
- <argument>-classpath</argument>
15 <classpath />
- <argument>
- -Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=
true
- </argument>
- <argument>
20 net.tuto2.ws.client.App
- </argument>
- </arguments>
- </configuration>
- </plugin>

7. Now you can execute your client via the mvn exec:exec command ;-).

David Bromberg Base revision 9e79146 www.david.bromberg.fr

Das könnte Ihnen auch gefallen