📜  Spring WS-编写客户端(1)

📅  最后修改于: 2023-12-03 14:47:34.063000             🧑  作者: Mango

Spring WS-编写客户端

Spring Web Services (Spring-WS)是一个基于Spring的web服务框架,是为了在基于SOAP的环境中开发Web服务。它提供了一个简单的方法来开发在基于XML的Web服务中的端点和客户端。本文将介绍如何在Spring-WS中编写一个客户端。

步骤
  1. 引入Spring-WS及相关的依赖库
<dependency>
     <groupId>org.springframework.ws</groupId>
     <artifactId>spring-ws-core</artifactId>
     <version>3.0.0.RELEASE</version>
   </dependency>
  1. 编写客户端配置文件

创建一个名为spring-ws-config.xml的XML配置文件,并按照以下示例代码进行配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:util="http://www.springframework.org/schema/util"
  xmlns:sws="http://www.springframework.org/schema/web-services"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd">

  <sws:annotation-driven/>

  <sws:dynamic-wsdl id="people"
    portTypeName="PeopleService"
    targetNamespace="http://www.example.com/people"
    locationUri="/ws/people/"/>

  <bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"/>

  <bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
    <constructor-arg ref="messageFactory"/>
    <property name="defaultUri" value="http://localhost:8080/spring4ws/services"/>
  </bean>

</beans>
  1. 编写客户端代码
@Endpoint
public class PeopleServiceClient {

  private static final String NAMESPACE_URI = "http://www.example.com/people";

  private WebServiceTemplate webServiceTemplate;

  @Autowired
  public void setWebServiceTemplate(WebServiceTemplate webServiceTemplate) {
    this.webServiceTemplate = webServiceTemplate;
  }

  @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getPersonRequest")
  @ResponsePayload
  public GetPersonResponse getPerson(@RequestPayload GetPersonRequest request) {
    return (GetPersonResponse) webServiceTemplate.marshalSendAndReceive(request);
  }
}
  1. 编写测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring-ws-config.xml"})
public class PeopleServiceClientTest {

  @Autowired
  private WebServiceTemplate webServiceTemplate;

  @Test
  public void testGetPerson() {
    GetPersonRequest request = new GetPersonRequest();
    request.setName("John Doe");
    GetPersonResponse response = (GetPersonResponse) webServiceTemplate.marshalSendAndReceive(request);
    Assert.assertNotNull(response);
    Assert.assertEquals("John", response.getPerson().getFirstName());
    Assert.assertEquals("Doe", response.getPerson().getLastName());
  }
}
总结

本文介绍了如何在Spring-WS中编写一个客户端。通过以上步骤,您可以轻松地为Web服务创建客户端,并在应用程序中使用它们。