使用Rest-assured从具有多个名称空间的SOAP XML响应中提取值的步骤如下:
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
Response response = given()
.contentType("text/xml")
.body(requestBody)
.when()
.post("/soap-endpoint")
.then()
.extract().response();
@XmlRootElement(name = "Envelope", namespace = "http://schemas.xmlsoap.org/soap/envelope/")
@XmlAccessorType(XmlAccessType.FIELD)
public class SoapEnvelope {
@XmlElement(name = "Body", namespace = "http://schemas.xmlsoap.org/soap/envelope/")
private SoapBody body;
// getters and setters
}
@XmlAccessorType(XmlAccessType.FIELD)
public class SoapBody {
@XmlElement(name = "Response", namespace = "http://example.com/namespace1")
private Response response;
// getters and setters
}
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {
@XmlElement(name = "Value", namespace = "http://example.com/namespace2")
private String value;
// getters and setters
}
JAXBContext jaxbContext = JAXBContext.newInstance(SoapEnvelope.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
SoapEnvelope soapEnvelope = (SoapEnvelope) unmarshaller.unmarshal(new StringReader(response.getBody().asString()));
String extractedValue = soapEnvelope.getBody().getResponse().getValue();
通过以上步骤,你可以使用Rest-assured从具有多个名称空间的SOAP XML响应中提取所需的值。请注意,以上示例中的名称空间和元素仅供参考,实际情况中需要根据具体的XML结构进行调整。对于更复杂的XML结构,可能需要创建更多的Java类来表示不同的元素和名称空间。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云