如何创建可动态处理请求并产生响应的SOAP Servlet?

问题描述 投票:0回答:1

我需要创建一个Java SOAP服务,但是我无法很好地访问将部署该服务的环境。只允许创建简单的Servlet。我可以在我的项目中包含库,但不能包含静态xml文件(例如WSDL文件)等。

因此,我正在寻找一些可以接受HttpServletRequest的库,或者动态生成WSDL xml(如果请求是GET并在查询字符串中包含?wsdl),或者处理传入的SOAP请求(如果请求是POST) 。

理想情况下,我想为此使用jws注释。

所以这是我的示例代码:

服务接口:

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

@WebService
public interface IMyService {
    @WebMethod
    public String echo(String string);
}

服务实施:

public class MyService implements IMyService {

    @Override
    public String echo(String string) {
        return string;
    }
}

Servlet:

public class TheService extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        // CODE HERE TO HANDLE THE GET. 
        // IF REQUEST CONTAINS ?wsdl, IT SHOULD DYNAMICALLY BUILD THE WSDL XML AND SERVICE IT

        xml = someSOAPLib.produceWSDL(request);

        response.setContentType("application/xml");

        outputStream = response.getOutputStream();
        outputStream.write(xml.getBytes());

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        // CODE HERE TO HANDLE THE POST. 
        // IT SHOULD PROCESS THE SOAP REQUEST AND RETURN A SOAP RESPONSE

        xml = someSOAPLib.processSOAPRequest(request);

        response.setContentType("application/xml");

        outputStream = response.getOutputStream();
        outputStream.write(xml.getBytes());

    }
}

我想Apache CXF或AXIS都可以做到这一点,但我找不到有关如何执行此操作的任何文档/示例代码。

java web-services soap cxf axis2
1个回答
0
投票

例如,您可以使用metro库来通过在线生成wsdl模式来构建基于类的Web服务,但是无论如何,sun-jaxws.xml文件是必需的。


示例Web服务:执行非常简单的操作。它需要两个整数,将它们相加,然后返回结果。

项目结构:

example-web-service
│
├── src
│   └── main
│       ├── java
│       │   └── ws
│       │       └── Calculator.java
│       └── webapp
│           └── WEB-INF
│               └── sun-jaxws.xml
└── pom.xml

Calculator.java

package ws;

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

@WebService(name = "CalculatorWS", targetNamespace = "http://example.com.ws/")
public class Calculator {
    @WebMethod(action="add")
    public int add(@WebParam(name = "i") int i,
                   @WebParam(name = "j") int j) {
        int k = i + j;
        return k;
    }
}

sun-jaxws.xml

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

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>example-web-service</groupId>
    <artifactId>example-web-service</artifactId>
    <name>example-web-service</name>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <java.version>1.6</java.version>
    </properties>

    <build>
        <finalName>example-web-service</finalName>
        <defaultGoal>package</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.metro</groupId>
            <artifactId>webservices-rt</artifactId>
            <version>2.4.4</version>
        </dependency>
    </dependencies>
</project>

客户请求:http://localhost:8080/calculator?wsdl

<?xml version='1.0' encoding='UTF-8'?>
<!-- Published by JAX-WS RI (https://github.com/eclipse-ee4j/metro-jax-ws).
    RI's version is Metro/2.4.4 (RELEASE-2.4.4-ce05bec; 2020-04-17T12:44:48+0000)
    JAXWS-RI/2.3.3 JAXWS-API/2.3.3 JAXB-RI/2.3.3 JAXB-API/2.3.3 git-revision#unknown. -->
<!-- Generated by JAX-WS RI (https://github.com/eclipse-ee4j/metro-jax-ws).
    RI's version is Metro/2.4.4 (RELEASE-2.4.4-ce05bec; 2020-04-17T12:44:48+0000)
    JAXWS-RI/2.3.3 JAXWS-API/2.3.3 JAXB-RI/2.3.3 JAXB-API/2.3.3 git-revision#unknown. -->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
             xmlns:wsp="http://www.w3.org/ns/ws-policy"
             xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy"
             xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://example.com.ws/"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/"
             targetNamespace="http://example.com.ws/" name="CalculatorService">
<types>
    <xsd:schema>
        <xsd:import namespace="http://example.com.ws/"
                    schemaLocation="http://localhost:8080/calculator?xsd=1" />
    </xsd:schema>
</types>
<message name="add">
    <part name="parameters" element="tns:add" />
</message>
<message name="addResponse">
    <part name="parameters" element="tns:addResponse" />
</message>
<portType name="CalculatorWS">
    <operation name="add">
        <input wsam:Action="add" message="tns:add" />
        <output wsam:Action="http://example.com.ws/CalculatorWS/addResponse" message="tns:addResponse" />
    </operation>
</portType>
<binding name="CalculatorWSPortBinding" type="tns:CalculatorWS">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
    <operation name="add">
        <soap:operation soapAction="add" />
        <input>
            <soap:body use="literal" />
        </input>
        <output>
            <soap:body use="literal"/>
        </output>
    </operation>
</binding>
<service name="CalculatorService">
    <port name="CalculatorWSPort" binding="tns:CalculatorWSPortBinding">
        <soap:address location="http://localhost:8080/calculator" />
    </port>
</service>
</definitions>

客户请求:http://localhost:8080/calculator?xsd=1

<?xml version='1.0' encoding='UTF-8'?>
<!-- Published by JAX-WS RI (https://github.com/eclipse-ee4j/metro-jax-ws).
    RI's version is Metro/2.4.4 (RELEASE-2.4.4-ce05bec; 2020-04-17T12:44:48+0000)
    JAXWS-RI/2.3.3 JAXWS-API/2.3.3 JAXB-RI/2.3.3 JAXB-API/2.3.3 git-revision#unknown. -->
<xs:schema xmlns:tns="http://example.com.ws/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           version="1.0" targetNamespace="http://example.com.ws/">

<xs:element name="add" type="tns:add" />
<xs:element name="addResponse" type="tns:addResponse" />

<xs:complexType name="add">
    <xs:sequence>
        <xs:element name="i" type="xs:int" />
        <xs:element name="j" type="xs:int" />
    </xs:sequence>
</xs:complexType>

<xs:complexType name="addResponse">
    <xs:sequence>
        <xs:element name="return" type="xs:int" />
    </xs:sequence>
</xs:complexType>
</xs:schema>
© www.soinside.com 2019 - 2024. All rights reserved.