您的位置:

使用SpringBoot调用Web Service接口

一、什么是Web Service

Web Service是一种基于Web的技术,允许两个应用程序之间进行交互,而无需了解对方应用程序的语言、操作系统和网络协议。Web Service采用标准的XML消息机制,实现跨平台、跨语言和跨网络的通信。

二、SpringBoot框架

SpringBoot是一种基于Spring框架的开发方式,通过简化配置文件的方式,使得我们可以快速地构建Spring应用程序。SpringBoot直接嵌入Tomcat容器,可以直接运行成为一个独立的应用程序。

三、调用Web Service接口的流程

使用SpringBoot调用Web Service接口,通常需要经过以下几个步骤:

1、导入WebService客户端

在pom.xml文件中加入WebService客户端的依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>

2、创建Web Service客户端代理类

使用JaxWsProxyFactoryBean类创建Web Service客户端代理类:

@Configuration
public class WebServiceConfig {

    @Bean(name = "webServiceTemplate")
    public WebServiceTemplate webServiceTemplate(Jaxb2Marshaller marshaller) {
        WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
        webServiceTemplate.setMarshaller(marshaller);
        webServiceTemplate.setUnmarshaller(marshaller);
        webServiceTemplate.setMessageSender(httpUrlConnectionMessageSender());
        return webServiceTemplate;
    }

    @Bean(name="httpUrlConnectionMessageSender")
    public HttpUrlConnectionMessageSender httpUrlConnectionMessageSender() {
        return new HttpUrlConnectionMessageSender();
    }

    @Bean
    public Jaxb2Marshaller marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("com.example.demo");
        return marshaller;
    }

    @Bean(name="testServiceClient")
    public TestService testServiceClient(Jaxb2Marshaller marshaller) {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(TestService.class);
        factory.setAddress("http://localhost:8080/services/TestService");
        factory.setServiceName(new QName("http://example.com/", "TestServicePort"));
        factory.setEndpointName(new QName("http://example.com/", "TestServiceSoap"));
        factory.setBindingId(SOAPBinding.SOAP11HTTP_BINDING);
        factory.setUnmarshaller(marshaller);
        factory.setMarshaller(marshaller);

        Object obj = factory.create();
        return (TestService) obj;
    }

}

3、调用Web Service接口

在控制器中使用Web Service客户端代理类调用Web Service接口:

@Controller
public class TestController {

    @Autowired
    private TestService testServiceClient;

    @RequestMapping("/test")
    @ResponseBody
    public String test() {
        TestRequest request = new TestRequest();
        request.setParam("test");
        TestResponse response = testServiceClient.test(request);
        return response.getResult();
    }

}

四、示例代码

下面是一个完整的Demo代码:

1、创建Web Service服务端

创建一个Java项目,添加以下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>

创建接口TestService:

@WebService
public interface TestService {
    @WebMethod(operationName = "test")
    TestResponse test(@WebParam(name = "request") TestRequest request);
}

创建请求类TestRequest:

@XmlRootElement(name="TestRequest")
@XmlAccessorType(XmlAccessType.FIELD)
public class TestRequest implements Serializable {

    private static final long serialVersionUID = 1L;

    private String param;

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }

}

创建响应类TestResponse:

@XmlRootElement(name="TestResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class TestResponse implements Serializable {

    private static final long serialVersionUID = 1L;

    private String result;

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

}

实现TestService接口:

@WebService(endpointInterface = "com.example.demo.TestService")
@Component
public class TestServiceImpl implements TestService {

    @Override
    public TestResponse test(TestRequest request) {
        TestResponse response = new TestResponse();
        String result = "Hello " + request.getParam();
        response.setResult(result);
        return response;
    }

}

2、创建SpringBoot应用程序客户端

创建一个SpringBoot项目,添加以下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>

编写启动类:

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

配置WebService客户端:

@Configuration
public class WebServiceConfig {

    @Bean(name = "webServiceTemplate")
    public WebServiceTemplate webServiceTemplate(Jaxb2Marshaller marshaller) {
        WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
        webServiceTemplate.setMarshaller(marshaller);
        webServiceTemplate.setUnmarshaller(marshaller);
        webServiceTemplate.setMessageSender(httpUrlConnectionMessageSender());
        return webServiceTemplate;
    }

    @Bean(name="httpUrlConnectionMessageSender")
    public HttpUrlConnectionMessageSender httpUrlConnectionMessageSender() {
        return new HttpUrlConnectionMessageSender();
    }

    @Bean
    public Jaxb2Marshaller marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("com.example.demo");
        return marshaller;
    }

    @Bean(name="testServiceClient")
    public TestService testServiceClient(Jaxb2Marshaller marshaller) {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(TestService.class);
        factory.setAddress("http://localhost:8080/services/TestService");
        factory.setServiceName(new QName("http://example.com/", "TestServicePort"));
        factory.setEndpointName(new QName("http://example.com/", "TestServiceSoap"));
        factory.setBindingId(SOAPBinding.SOAP11HTTP_BINDING);
        factory.setUnmarshaller(marshaller);
        factory.setMarshaller(marshaller);

        Object obj = factory.create();
        return (TestService) obj;
    }

}

编写控制器:

@Controller
public class TestController {

    @Autowired
    private TestService testServiceClient;

    @RequestMapping("/test")
    @ResponseBody
    public String test() {
        TestRequest request = new TestRequest();
        request.setParam("test");
        TestResponse response = testServiceClient.test(request);
        return response.getResult();
    }

}

五、总结

本文介绍了如何使用SpringBoot调用Web Service接口的方法,从Web Service的基本概念、SpringBoot框架、调用Web Service接口的流程和示例代码四个方面进行了详细的阐述。