]
一.创建两个springBoot+gradle项目,将以下依赖放入build.gradle中
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
//引入cxf框架
compile 'org.apache.cxf:cxf-spring-boot-starter-jaxws:3.2.6'
}
二.首先创建webService服务端
1.创建一个UserService接口(注意加上@webService才能成为服务端)
@WebService(name = "UserService", // 暴露服务名称
targetNamespace = "http://service.wsserver.meng.com" // 命名空间,一般是接口的包名倒序
)
public interface UserService {
@WebMethod//标注该方法为webservice暴露的方法,用于向外公布,它修饰的方法是webservice方法,去掉也没影响的,类似一个注释信息。
@WebResult(name="int",targetNamespace="")//name属性为返回值类型,targetNamespace可以为空
//模拟两个数相加
public int add(@WebParam(name = "w1")int w1,@WebParam(name = "w2")int w2);
}
2.创建UserService的实现类UserServiceImpl
@WebService(serviceName="UserService",//对外发布的服务名
targetNamespace = "http://service.wsserver.meng.com",//指定你想要的名称空间,通常使用使用包名反转
endpointInterface="com.meng.wsserver.service.UserService")//服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口
@Component
public class UserServiceImpl implements UserService {
//业务逻辑
@WebMethod(operationName = "add") // 指定方法名
@Override
public int add(int w1, int w2) {
return w1+w2;
}
}
3.创建配置发布服务类SendServer
@Configuration
public class SendServer {
@Autowired
private Bus bus;
@Autowired
UserService userService;
/**
* 此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使用ipconfig查看本机ip来访问
* 此方法被注释后:wsdl访问地址为http://127.0.0.1:8080/services/user?wsdl
* 去掉注释后:wsdl访问地址为:http://127.0.0.1:8080/soap/user?wsdl
*
* 或者 CXF默认的服务路径是"/services/**"。如果你想改变它的url直接在在application.properties中修改 cxf.path=/soap
*/
@SuppressWarnings("all")
@Bean
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
}
/** JAX-WS
* 站点服务
* **/
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, userService);
endpoint.publish("/user");
return endpoint;
}
}
二.创建webService客户端
1.创建客户端类client_demo
package com.hao.c_demo.c_test;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
public class client_demo {
public static void main(String[] args) throws Exception{
//采用动态工厂方式 不需要指定服务接口
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://127.0.0.1:8080/soap/user?wsdl");
Object[] objects = new Object[0];
try {
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("add", 1,2);
System.out.println("返回数据:" + objects[0]);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
}
/*
第二种方式 需要指定命名空间
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf
.createClient("http://127.0.0.1:8080/soap/user?wsdl");//参数为服务端发布的地址
QName qName = new QName("http://service.wsserver.meng.com", "add");//QName就是创建xml的元素,第一个参数为方法的命名空间,第二个为方法名
try {
Object[] objects = new Object[0];
// invoke("方法名",参数1,参数2,参数3....);
objects =client.invoke(qName,1,2);
System.out.println(objects[0]);
} catch (Exception e) {
e.printStackTrace();
}
*/
2. 查看运行结果:
三.需要注意的问题
1. 如果使用的是jdk1.8则需要看一下在jdk1.8.0/jre/lib目录下有没有一个jaxp.properties文件,如果没有则创建一个,其文件内容为 javax.xml.accessExternalSchema = all
否则会报错(java.lang.AssertionError: org.xml.sax.SAXParseException; systemId: jar:file:/path/to/glassfish/modules/jaxb-osgi.jar!/com/sun/tools/xjc/reader/xmlschema/bindinfo/binding.xsd; lineNumber: 52; columnNumber: 88; schema_reference: Failed to read schema document ‘xjc.xsd’, because ‘file’ access is not allowed due to restriction set by the accessExternalSchema property)
2.使用springboot结合cfx框架时要对应好版本号
parent版本是1.4.5,同时jaxws版本是3.1.7时;
parent版本是1.4.5,同时jaxws版本是3.1.11时;
parent版本是1.5.6,同时jaxws版本是3.1.11时;
parent版本是1.5.8,同时jaxws版本是3.1.12时;
parent版本是1.5.9,同时jaxws版本是3.1.11时;
parent版本是1.5.9,同时jaxws版本是3.1.12时;
parent版本是2.0.3,同时jaxws版本是3.2.6时;
spring boot 1.4 版本对应cxf-spring-boot-starter-jaxws 3.1.X 版本
spring boot 1.5 版本对应cxf-spring-boot-starter-jaxws 3.2.X 版本
3. CXF默认的服务路径是"/services/**"。如果你想改变它的url为/xxx/*。在application.properties中修改
cxf.path=/xxx
4.注解及其解释
@WebService (serviceName="PhoneManager",//修改服务名
targetNamespace="http://dd.ws.it.cn") //修改命名空间 ,默认包名,取反
//声明该业务类 对外提供webservice服务 ,默认只是对public 修饰的方法对外以webservice形式发布
public class PhoneService {
/**@WebMethod(operationName="getMObileInfo"): 修改方法名
* @WebResult(name="phone"):修改返回参数名
* @WebParam(name="osName"):修改输入参数名
*/
}