我需要创建一个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都可以做到这一点,但我找不到任何关于如何做到这一点的文档/示例代码。
例如,您可以使用 metro 库来构建一个基于类的 Web 服务,并在线生成wsdl
模式,但无论如何sun-jaxws.xml文件是必需的。
示例 Web 服务:执行非常简单的操作。它接受两个整数,将它们相加,然后返回结果。
项目结构:
example-web-service
│
├── src
│ └── main
│ ├── java
│ │ └── ws
│ │ └── Calculator.java
│ └── webapp
│ └── WEB-INF
│ └── sun-jaxws.xml
└── pom.xml
计算器.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;
}
}
太阳-贾克斯.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>
绒球.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>