版本说明:
Springboot是2.0.4.RELEASE用cxf的3.2.5版本

创建SpringBoot项目

添加maven依赖(pom.xml)

<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>
  
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
  </parent>
  
  <groupId>com.zmf</groupId>
  <artifactId>springboot-webservice</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
   <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
  
  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- http -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.4</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.41</version>
        </dependency>
        <!-- 热部署模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
        </dependency>

        <!-- CXF webservice -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.1.11</version>
        </dependency>
        <!-- CXF webservice -->

    </dependencies>

</project>

编写WebService接口

package com.zmf.webServiceTest;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService(name="TestService",//暴露的服务名
targetNamespace="http://webServiceTest.zmf.com"//命名空间,一般是接口包名的倒序
)
public interface TestService {
	
	@WebMethod
	@WebResult(name="String",targetNamespace="")
	public String sendMessage(@WebParam(name="username") String username);
	
}

编写接口实现类

package com.zmf.webServiceTest;

import javax.jws.WebService;

import org.springframework.stereotype.Component;

@WebService(name="TestService",//暴露的服务名
targetNamespace="http://webServiceTest.zmf.com",//命名空间,一般是接口包名的倒序
endpointInterface="com.zmf.webServiceTest.TestService"//接口地址
)
@Component
public class TestServiceImpl implements TestService{

	@Override
	public String sendMessage(String username) {
		return "Hello"+username;
	}

}

编写cxf配置类

package com.zmf.webServiceTest.cxfConfigs;

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.zmf.webServiceTest.TestService;

@Configuration
public class CxfConfig {
	
	@Autowired
	private Bus bus;
	
	@Autowired
	private TestService testService;
	
	@Bean
	public Endpoint endpoint() {
		EndpointImpl endpoint=new EndpointImpl(bus, testService);
		endpoint.publish("/TestService");
		return endpoint;
	}
	
}

默认服务在Host:port/services/* 路径下
将TestService接口发布在了路径/services/TestService下,wsdl文档路径为http://localhost:8080/services/TestService?wsdl

浏览器请求http://localhost:8080/services/TestService?wsdl
出现以下画面

springboot WxMaService配置_web service

最终项目结构如下

springboot WxMaService配置_spring boot_02

测试的cxf客户端

package com.zmf.webServiceTest.client;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.junit.Test;

public class TestClient {
	 @Test
    public void testSend1(){

        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8080/services/TestService?wsdl");

        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
        Object[] objects = new Object[0];
        try {

            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("sendMessage", "茂飞");
            String str=(String)objects[0];
            System.out.println("返回字符串:"+str);
            System.out.println("返回数据:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }
	
}

当中的MyInterceptor.java拦截器目前还不知道有什么用,不加也不影响功能。上面的过程是没有拦截器的。(可能是监控用的)

以下拦截器的代码

package com.zmf.webServiceTest.cxfConfigs;

import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.interceptor.AbstractLoggingInterceptor;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.io.DelegatingInputStream;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.Phase;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.logging.Logger;

public class MyInterceptor extends AbstractLoggingInterceptor {
//    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(MyInterceptor.class);
    public MyInterceptor() {
        super(Phase.RECEIVE);
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        InputStream is = message.getContent(InputStream.class);
        if(is!=null){
            CachedOutputStream bos = new CachedOutputStream();
            if (threshold > 0) {
                bos.setThreshold(threshold);
            }
            try {
                // use the appropriate input stream and restore it later使用适当的输入流,稍后将其还原
                InputStream bis = is instanceof DelegatingInputStream
                        ? ((DelegatingInputStream)is).getInputStream() : is;


                //only copy up to the limit since that's all we need to log我们只需要复制到最大限度,因为这就是我们需要记录的全部内容
                //we can stream the rest我们可以把剩下的流出来
                IOUtils.copyAtLeast(bis, bos, limit == -1 ? Integer.MAX_VALUE : limit);
                bos.flush();
                bis = new SequenceInputStream(bos.getInputStream(), bis);

                // restore the delegating input stream or the input stream还原委托输入流或输入流
                if (is instanceof DelegatingInputStream) {
                    ((DelegatingInputStream)is).setInputStream(bis);
                } else {
                    message.setContent(InputStream.class, bis);
                }

                bos.close();
            } catch (Exception e) {
                throw new Fault(e);
            }finally{
//                LOGGER.info(bos.toString());
            }
        }
    }

    @Override
    protected Logger getLogger() {
        // TODO Auto-generated method stub
        return null;
    }
}

调用拦截器的代码:在cxf配置类中(可调,可不调)

springboot WxMaService配置_spring boot_03


还有一个可能存在的问题

SpringBoot在配置WebService之后可能会出现在controller中写的get或post接口(http接口)不能访问情况。原因是SpringBoot默认注册的是dispatcherServlet,当手动配置ServletRegistrationBean之后就不会再去注册默认的dispatcherServlet,此时需要手动注册一个dispatcherServlet。

代码如下

/**
	 * 注册一个dispatcherServlet,解决增加ws之后https接口访问不了问题
	 */
	@Bean
	public ServletRegistrationBean restServlet(){
		//注解扫描上下文
		AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
		//base package
		applicationContext.scan("com.xbsafe");
		//通过构造函数指定dispatcherServlet的上下文
		DispatcherServlet rest_dispatcherServlet = new DispatcherServlet(applicationContext);

		//用ServletRegistrationBean包装servlet
		ServletRegistrationBean registrationBean = new ServletRegistrationBean(rest_dispatcherServlet);
		registrationBean.setLoadOnStartup(1);
		//指定urlmapping
		registrationBean.addUrlMappings("/*");
		//指定name,如果不指定默认为dispatcherServlet
		registrationBean.setName("rest");
		return registrationBean;
	}

这里咱们没有去手动注册任何servlet,不存在这个问题。以防以后有需求,先记录上这个方法,也是一种解决方案。