测试项目git地址(里面简单的做了出入参的转换操作):
https://github.com/zangdy/webservice-test.git
- 首先要引入cxf相关依赖
<!-- cxf start-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.4</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
<!-- cxf end-->
注: 这里我是引入了hibernate-validator,原因是我没有引入时报了以下错误。
2. 其次编写webservice接口及其方法
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
* @author zangdongyun
* @Desc webservice 测试类
*/
@WebService
public interface TestWebservice {
/**
* 测试
* @param requestData 业务入参
* @return
*/
@WebMethod
String test(@WebParam(name = "requestData") String requestData);
}
- 实现webservice接口
import com.shulil.webservice.TestWebservice;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
/**
* @author zangdongyun
* @Desc webservice 测试类
*/
@Component
@WebService(serviceName = "testWebservice",// 对外发布服务名
endpointInterface = "com.shulil.webservice.TestWebservice", // 所实现接口的路径
targetNamespace = "http://test.webservice.shuli.com" // 指定想要的名称空间,一般是该路径的反写方式
)
public class TestWebServiceImpl implements TestWebservice {
/**
* 测试
*
* @param requestData 业务入参
* @return
*/
@Override
public String test(String requestData) {
System.out.println(requestData);
return "成功";
}
}
- 最后一步编写webservice配置类
import com.shulil.webservice.TestWebservice;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
/**
* @author zangdongyun
* @Desc webservice配置类
*/
@Configuration
public class WebServiceConfg {
@Autowired
private Bus bus;
@Autowired
private TestWebservice testWebservice;
/**
* 可以通过配置该bean修改默认服务地址,如果不配置该bean,则默认服务是在host:port/services/发布地址 下
* @return
*/
@Bean
public ServletRegistrationBean servletRegistrationBean(){
return new ServletRegistrationBean(new CXFServlet(),"/webservice/*");
}
/**
* 配置CXF服务发布,默认服务是在host:port/services/发布地址
* 1、默认访问地址 http://127.0.0.1:8080/services/examine?wsdl
* 2、配置了ServletRegistrationBean后访问地址:http://127.0.0.1:8080/webservice/examine?wsdl
*/
@Bean
public Endpoint endpoint(){
EndpointImpl endpoint = new EndpointImpl(bus,testWebservice);
//发布地址
endpoint.publish("/examine");
return endpoint;
}
}
- 最后通过SoapUI工具测试,参数需要写在<![CDATA[参数]]> 里面