How do I generate a new SOAP webservice from Maven?

276 views
Skip to first unread message

theirman

unread,
Jan 18, 2021, 9:07:31 AM1/18/21
to Payara Forum
Hello

I have to produce documentation for my team. This documentation should show how to generate a webservice project from scratch, compile it and deploy it.

To realize this project, I wrote a simple project whose main files are :

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

  <modelVersion>4.0.0</modelVersion>

  <groupId>fr.inrae.sicpa</groupId>
  <artifactId>MyServiceWS</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>MyServiceWS</name>
  <!-- FIXME change it to the project's website -->

  <properties>
    <encoding>UTF-8</encoding>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>jakarta.jws</groupId>
      <artifactId>jakarta.jws-api</artifactId>
      <version>2.1.0</version>
    </dependency>
    <dependency>
      <groupId>jakarta.xml.bind</groupId>
      <artifactId>jakarta.xml.bind-api</artifactId>
      <version>2.3.3</version>
    </dependency>
    <dependency>
      <groupId>jakarta.xml.soap</groupId>
      <artifactId>jakarta.xml.soap-api</artifactId>
      <version>1.4.2</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>MyServiceWS</finalName>
    <directory>${basedir}/target</directory>
    <!-- main -->
    <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
    <outputDirectory>${basedir}/target/classes</outputDirectory>
    <!-- test -->
    <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
    <testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
    
    <resources>
      <resource>
        <directory>${basedir}/src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
          <include>*.properties</include>
          <include>*.xml</include>
        </includes>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.3</version>
          <configuration>
            <failOnMissingWebXml>false</failOnMissingWebXml>
            <warSourceDirectory>${basedir}/src/main/webapp/WebContent</warSourceDirectory>
            <warSourceExcludes>${basedir}/src/main/webapp/WEB-INF/web.xml</warSourceExcludes>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
  <display-name>MyServiceWS</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>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

src/main/webapp/WEB-INF/payara-web.xml
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE payara-web-app PUBLIC 
          "-//Payara.fish//DTD Payara Server 4 Servlet 3.0//EN" 
<payara-web-app>
    <context-root>/MyServiceWS</context-root>
</payara-web-app>


/src/main/java/fr/inrae/sicpa/services/IMyService.java
package fr.inrae.sicpa.services;

import javax.jws.WebService;

@WebService(name="IMyService", targetNamespace="http://services.sicpa.inra.fr")
public interface IMyService 
{
public double getPrixTTC( double ht );
public double getPrixHT( double ttc );
public double getTVA( double prix, boolean isTTC );
}

/src/main/java/fr/inrae/sicpa/services/MyServiceImpl.java
package fr.inrae.sicpa.services;

import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;

@WebService(serviceName="MyServiceWS", portName="MyServicePort", endpointInterface="fr.inrae.sicpa.MyServiceWS")
@SOAPBinding(style=Style.DOCUMENT, use=Use.LITERAL)
public class MyServiceImpl implements IMyService
{
private final static double TAUX = 0.196;
@WebMethod(operationName="getPrixTTC")
@WebResult(name = "ttc")
@Override
public double getPrixTTC(double ht) 
{
return ht * (1 + TAUX);
}

@WebMethod(operationName="getPrixHT")
@WebResult(name = "ht")
@Override
public double getPrixHT(double ttc) 
{
return ttc / (1 + TAUX);
}

@WebMethod(operationName="getTVA")
@WebResult(name = "tva")
@Override
public double getTVA(double prix, boolean isTTC) 
{
if(isTTC)
return prix - (prix/(1 + TAUX));
else
return prix * TAUX;
}
}

At this stage, I manage to generate my project structure, compile the project and deploy it on my payara server (5.192). But what I don't understand is that neither the wsdl file nor the endpoint are created at deployment time.

I've been struggling with this since the end of December. Where do you think my error lies?

Thank you very much for your help
Thierry

theirman

unread,
Jan 19, 2021, 3:10:32 AM1/19/21
to Payara Forum
I found the solution thanks to RagnarosLightLord on Discord :
Hi
There is an apache plugin named cxf-java2ws-plugin to pregenerate your wsdl file when compiling maven. 
Look on this side, it will help you
Good luck
RagnarosLightLord

Here is how to modify the pom.xml to be able to generate the wsdl file when compiling maven :
  <build>
    ...
    ...
    <plugins>
      <plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-java2ws-plugin</artifactId>
        <version>${cxf.version}</version>
        <dependencies>
          <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
          </dependency>
          <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-simple</artifactId>
            <version>${cxf.version}</version>
          </dependency>
        </dependencies>
        <executions>
          <execution>
            <id>generate-wsdl</id>
            <phase>process-classes</phase>
            <configuration>
              <attachWsdl>true</attachWsdl>
              <className>fr.inrae.sicpa.services.MyServiceImpl</className>
              <outputFile>${project.build.outputDirectory}/${project.build.finalName}.wsdl</outputFile>
              <!-- See here for options http://cxf.apache.org/docs/java-to-ws.html -->
              <databinding>jaxb</databinding>
              <frontend>jaxws</frontend>
              <genClient>false</genClient>
              <genServer>false</genServer>
              <genWrapperbean>false</genWrapperbean>
              <genWsdl>true</genWsdl>
              <quiet>false</quiet>
              <verbose>true</verbose>
            </configuration>
            <goals>
              <goal>java2ws</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
    ...
    ...
  </build>
Reply all
Reply to author
Forward
0 new messages