gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:45:26 25 4
gpt4 key购买 nike

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

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

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

所以这是我的示例代码:

服务接口(interface):

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

小服务程序:
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 文件是必要的。

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

项目结构:

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

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

关于java - 如何创建动态处理请求并产生响应的 SOAP Servlet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62308153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com