【问题标题】:How do I generate a new SOAP webservice from Maven?如何从 Maven 生成新的 SOAP 网络服务?
【发布时间】:2021-01-18 14:28:12
【问题描述】:

我必须为我的团队制作一份文档。该文档应该展示如何从头开始生成 web 服务项目,编译它并将其部署到 payara (glassfish) 服务器中

为了实现这个项目,我写了一个简单的项目,其主要文件是:

pom.xml

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

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <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 -->
  <url>http://www.example.com</url>

  <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>
    <!-- https://mvnrepository.com/artifact/jakarta.jws/jakarta.jws-api -->
    <dependency>
      <groupId>jakarta.jws</groupId>
      <artifactId>jakarta.jws-api</artifactId>
      <version>2.1.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/jakarta.xml.bind/jakarta.xml.bind-api -->
    <dependency>
      <groupId>jakarta.xml.bind</groupId>
      <artifactId>jakarta.xml.bind-api</artifactId>
      <version>2.3.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/jakarta.xml.soap/jakarta.xml.soap-api -->
    <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>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <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>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <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"?>
<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_2_5.xsd" version="2.5">
  <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" 
          "https://raw.githubusercontent.com/payara/Payara-Server-Documentation/master/schemas/payara-web-app_4.dtd">
<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;
    }
}

在这个阶段,我设法生成我的项目结构,编译项目并将其部署在我的 payara 服务器 (5.192) 上。但我不明白的是,wsdl 文件和端点都不是在部署时创建的。

自 12 月底以来,我一直在为此苦苦挣扎。你认为我的错误在哪里?

非常感谢您的帮助

蒂埃里

【问题讨论】:

    标签: maven web-services soap glassfish payara


    【解决方案1】:

    感谢 RagnarosLightLord on Discord,我找到了解决方案:

    嗨 有一个名为 cxf-java2ws-plugin 的 apache 插件来预生成 编译 maven 时的 wsdl 文件。
    看看这边,它会帮助你 祝你好运 拉格纳罗斯光王

    这里是如何修改 pom.xml 以便在编译 maven 时能够生成 wsdl 文件:

      <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>
    

    【讨论】:

      猜你喜欢
      • 2012-09-06
      • 1970-01-01
      • 2017-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多