1、首先创建Spring Boot项目,在pom.xml添加CXF包支持
<!-- 添加 CXF --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.2.5</version> </dependency>
2、项目目录
3、编写WebService
CommonService.java 和 CommonServiceImpl.java
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService(name = "CommonService", // 暴露服务名称
targetNamespace = "http://service.webservice.cma.jsaisino.com/")//命名空间,一般是interface的包名倒序
public interface CommonService {
@WebMethod //标注该方法为webservice暴露的方法,用于向外公布,它修饰的方法是webservice方法,去掉也没影响的,类似一个注释信息。
@WebResult(name = "String", targetNamespace = "")
public String common_world(@WebParam(name = "userName") String name);
//不同的方法
@WebMethod
@WebResult(name = "String", targetNamespace = "")
public String common_say(@WebParam(name = "userName") String name);
}
import com.jsaisino.cma.webservice.service.CommonService;
import org.springframework.stereotype.Component;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService( serviceName = "CommonService", //对外发布的服务名
targetNamespace = "http://service.webservice.cma.jsaisino.com/",//指定想要的名称空间,通常使用interface接口包名反转
endpointInterface = "com.jsaisino.cma.webservice.service.CommonService")//服务接口interface全路径, 服务端点接口
@Component //一般不会在接口上放注解,在实现类上注解,这样才符合热插拔式,如果后期更换实现类,直接注解其他实现类就可以。
public class CommonServiceImpl implements CommonService {
@Override
public String common_world(String name) {
return "common_world ," + name;
}
@WebMethod(exclude=false) //默认fase: 表示发布该方法 true:表示不发布此方法
@Override
public String common_say(String name) {
return "common_say ," + name;
}
}
接口与接口实现类关系
HelloService.java 和 HelloServiceImpl.java
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService(name = "HelloService", //暴露服务名称。
targetNamespace = "http://service.webservice.cma.jsaisino.1com/")//命名空间,一般是接口(interface)的包名倒序
public interface HelloService {
@WebResult(name = "String", targetNamespace = "")
public String hello_world(@WebParam(name = "myname") String myname);
}
import com.jsaisino.cma.webservice.service.HelloService;
import javax.jws.WebService;
//@WebService表示该类是一个服务类,需要发布其中的public的方法
@WebService(serviceName = "HelloService", // 该webservice服务的名称,与接口中指定的name一致,对外发布的服务名
targetNamespace = "http://service.webservice.cma.jsaisino.1com/", //名称空间,通常使用接口包名反转
endpointInterface = "com.jsaisino.cma.webservice.service.HelloService") //服务接口全路径
public class HelloServiceImpl implements HelloService {
@Override
public String hello_world(String myname) {
//Thread.currentThread().sleep(2000);
return "hello_world ," + myname;
}
}
接口与接口实现类关系
4、编写CXF配置文件
import com.jsaisino.cma.webservice.service.CommonService;
import com.jsaisino.cma.webservice.service.impl.HelloServiceImpl;
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 javax.xml.ws.Endpoint;
@Configuration
public class CxfConfig {
@Autowired
private Bus bus; //private SpringBus bus;
@Autowired
private CommonService commonService;
// 配置CXF服务发布,默认服务是在host:port/services/发布地址
// 访问地址 http://127.0.0.1:8080/Service/common?wsdl
@Bean
public Endpoint another_endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, commonService);
endpoint.publish("/common"); //发布地址
return endpoint;
}
// 访问地址 http://127.0.0.1:8080/Service/hello?wsdl
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, new HelloServiceImpl());
endpoint.publish("/hello"); //发布地址
return endpoint;
}
}
5、配置cxf服务发布
CXF默认的服务路径是"/services/**"。如果想改变它的url。在application.properties中修改:
两个独立webservice服务访问地址:
http://127.0.0.1:8080/Service/common?wsdl
http://127.0.0.1:8080/Service/hello?wsdl
6、CXF客户端代码
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
public class AllClient {
public static void main(String[] args) {
client1();
client2();
client3();
}
// 创建动态客户端
public static void client1() {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8080/Service/common?wsdl");
Object[] objects = new Object[0];
try {
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("common_world", "zhangsan123456");
System.out.println("返回数据:" + objects[0]);
client.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
// 创建动态客户端
public static void client2() {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8080/Service/common?wsdl");
Object[] objects = new Object[0];
try {
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("common_say", "zhangsan123456");
System.out.println("返回数据:" + objects[0]);
client.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
// 创建动态客户端
public static void client3() {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8080/Service/hello?wsdl");
Object[] objects = new Object[0];
try {
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("hello_world", "zhangsan123456");
System.out.println("返回数据:" + objects[0]);
client.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果:
返回数据:common_world ,zhangsan123456
返回数据:common_say ,zhangsan123456
返回数据:hello_world ,zhangsan123456