java - 通过SOAP操作生成SOAP服务

标签 java eclipse web-services soap axis2

由于我一直使用REST,所以我是SOAP服务的新手,所以这个问题可能会让您感到愚蠢,我只需要对这种技术进行一些指导,因为我不知道从哪里开始。

因此,我有一个可供其他软件使用的SOAP服务,并且该软件显示警告:缺少肥皂操作。

据我所知,soap操作是标头的属性,但是目前正在生成没有任何标头的wsdl文件,如下所示:

    <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns="http://foo.bar.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" targetNamespace="http://adomingo.gdxgroup.com">
<wsdl:documentation> Please Type your service description here </wsdl:documentation>
<wsdl:types>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://foo.bar.com">
<xs:element name="version">
<xs:complexType>
<xs:sequence/>
</xs:complexType>
</xs:element>
<xs:element name="versionResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="methodName">


我要部署此服务的工作是使用Axis2和Tomcat 9.0在Eclipse中的POJO类中(在动态Web项目内部)右键单击“创建Web服务”。当一切正常时,我将其导出为战争文件。

这一直有效,直到软件的版本升级为止,此版本要求此SOAP操作用于自定义Web服务。 (Docuware 7.1)

所以我的问题是,我应该使用哪种技术来生成带有标头及其肥皂操作的Web服务?

最佳答案

您正在寻找JAX-WS规范。最小的网络服务器可以是:

import javax.jws.WebService;
import javax.jws.WebMethod;

@WebService
public class FooWebServer {

    public void FooWebServer() {
    }

    @WebMethod
    public String barMethod(String args) {
         return args.toLowerCase();
    }
}


并且应使用Tomcat运行(sun-jaxws.xml文件夹中可能需要web.xmlWEB-INF)。其他使用EJB的方法非常普遍。

您可以在此处https://docs.oracle.com/javaee/6/tutorial/doc/bnayl.html了解有关JAX-WS的信息。

最终的实现,依赖项等取决于您的要求,但完整的功能示例可能是:

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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>wstest</groupId>
    <artifactId>wstest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>embeddedTomcatSample Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <tomcat.version>8.5.23</tomcat.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>javax.xml.ws</groupId>
            <artifactId>jaxws-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>jaxws-rt</artifactId>
            <version>2.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jasper</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jasper-el</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jsp-api</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>embeddedTomcatSample</finalName>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>appassembler-maven-plugin</artifactId>
                <version>2.0.0</version>
                <configuration>
                    <assembleDirectory>target</assembleDirectory>
                    <programs>
                        <program>
                            <mainClass>launch.Main</mainClass>
                            <name>webapp</name>
                        </program>
                    </programs>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>assemble</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>


FooWebServer.java

package com.foo;

import javax.jws.WebService;
import javax.jws.WebMethod;

@WebService(name="foo", serviceName="foows")
public class FooWebServer {

    public void FooWebServer() {
    }

    @WebMethod
    public String barMethod(String args) {
        return args.toLowerCase();
    }
}


Main.java

package com.foo;

import java.io.File;

import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;

public class Main {

    public static void main(String[] args) throws Exception {

        String webappDirLocation = "src/main/webapp/";
        Tomcat tomcat = new Tomcat();

        //The port that we should run on can be set into an environment variable
        //Look for that variable and default to 8080 if it isn't there.
        String webPort = System.getenv("PORT");
        if(webPort == null || webPort.isEmpty()) {
            webPort = "8080";
        }

        tomcat.setPort(Integer.valueOf(webPort));

        StandardContext ctx = (StandardContext) tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
        System.out.println("configuring app with basedir: " + new File("./" + webappDirLocation).getAbsolutePath());

        // Declare an alternative location for your "WEB-INF/classes" dir
        // Servlet 3.0 annotation will work
        File additionWebInfClasses = new File("target/classes");
        WebResourceRoot resources = new StandardRoot(ctx);
        resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes",
                additionWebInfClasses.getAbsolutePath(), "/"));
        ctx.setResources(resources);

        tomcat.start();
        tomcat.getServer().await();
    }
}


src/main/webapp/WEB-INF下:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <listener>
        <listener-class>
            com.sun.xml.ws.transport.http.servlet.WSServletContextListener
        </listener-class>
    </listener>
    <servlet>
        <servlet-name>services</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>services</servlet-name>
        <url-pattern>/services</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>


sun-jaxws.xml

<?xml version="1.0" encoding="UTF-8"?>
<endpoints version="2.0" xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
    <endpoint implementation="com.foo.FooWebServer" name="fooSrv" url-pattern="/foo"/>
</endpoints>


现在,您可以运行您的主类并转到显示以下信息的“ webservice主页” http://localhost:8080/foo

服务端点:http://localhost:8080/foo

WSDL定义:http://localhost:8080/foo?wsdl

使用SOAP UI并提供该wsdl地址,您可以创建客户端并使用以下请求进行测试:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:foo="http://foo.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <foo:barMethod>
         <arg0>This is a simple test</arg0>
      </foo:barMethod>
   </soapenv:Body>
</soapenv:Envelope>


您将获得预期的响应:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:barMethodResponse xmlns:ns2="http://foo.com/">
         <return>this is a simple test</return>
      </ns2:barMethodResponse>
   </S:Body>
</S:Envelope>

关于java - 通过SOAP操作生成SOAP服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57051538/

相关文章:

Java循环执行错误

java - 带有 Hotswap 代理的 Weblogic 服务器

java - 如何在 Eclipse 中获取 Javafx 9?

java - 在类中使用空方法

java - 如何在 JGit 中 merge ?

C++ Eclipse 控制台将纯文本编码为其他字符

java - 找不到资源可运行的jar

asp.net - 使用 JQuery 和 JSON 来使用 Asp.net 服务器端异步 Web 方法

web-services - 何时使用 BPEL 和 ESB?

.net - WCF - 已超过传入消息的最大消息大小配额