diff --git a/spring-boot-webservice/pom.xml b/spring-boot-webservice/pom.xml new file mode 100644 index 0000000..871cb20 --- /dev/null +++ b/spring-boot-webservice/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.4.RELEASE + + + xyz.zhouzhaodong + spring-boot-webservice + 0.0.1-SNAPSHOT + spring-boot-webservice + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.apache.cxf + cxf-spring-boot-starter-jaxws + 3.4.0 + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot-webservice/src/main/java/xyz/zhouzhaodong/springbootwebservice/SpringBootWebserviceApplication.java b/spring-boot-webservice/src/main/java/xyz/zhouzhaodong/springbootwebservice/SpringBootWebserviceApplication.java new file mode 100644 index 0000000..1b9aebc --- /dev/null +++ b/spring-boot-webservice/src/main/java/xyz/zhouzhaodong/springbootwebservice/SpringBootWebserviceApplication.java @@ -0,0 +1,13 @@ +package xyz.zhouzhaodong.springbootwebservice; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootWebserviceApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootWebserviceApplication.class, args); + } + +} diff --git a/spring-boot-webservice/src/main/java/xyz/zhouzhaodong/springbootwebservice/client/CxfClient.java b/spring-boot-webservice/src/main/java/xyz/zhouzhaodong/springbootwebservice/client/CxfClient.java new file mode 100644 index 0000000..39a338c --- /dev/null +++ b/spring-boot-webservice/src/main/java/xyz/zhouzhaodong/springbootwebservice/client/CxfClient.java @@ -0,0 +1,66 @@ +package xyz.zhouzhaodong.springbootwebservice.client; + +import org.apache.cxf.endpoint.Client; +import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; +import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; +import xyz.zhouzhaodong.springbootwebservice.service.CommonService; + +import java.util.Arrays; +import java.util.List; + +/** + * @author zhouzhaodong + */ +public class CxfClient { + + public static void main(String[] args) { + cl2(); + } + + /** + * 方式1.代理类工厂的方式,需要拿到对方的接口 + */ + public static void cl1() { + try { + // 接口地址 + String address = "http://localhost:8080/services/CommonService?wsdl"; + // 代理工厂 + JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean(); + // 设置代理地址 + jaxWsProxyFactoryBean.setAddress(address); + // 设置接口类型 + jaxWsProxyFactoryBean.setServiceClass(CommonService.class); + // 创建一个代理接口实现 + CommonService cs = (CommonService) jaxWsProxyFactoryBean.create(); + // 数据准备 + String userName = "Leftso"; + // 调用代理接口的方法调用并返回结果 + String result = cs.sayHello(userName); + System.out.println("返回结果:" + result); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 方式2.动态调用方式 + */ + public static void cl2() { + + // 创建动态客户端 + JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); + Client client = dcf.createClient("http://localhost:8080/services/CommonService?wsdl"); + // 需要密码的情况需要加上用户名和密码 + // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD)); + Object[] objects; + try { + objects = client.invoke("sayHello", "1"); + List objects1 = Arrays.asList(objects); + System.out.println(objects1.get(0)); + } catch (java.lang.Exception e) { + e.printStackTrace(); + } + + } + +} diff --git a/spring-boot-webservice/src/main/java/xyz/zhouzhaodong/springbootwebservice/config/CxfConfig.java b/spring-boot-webservice/src/main/java/xyz/zhouzhaodong/springbootwebservice/config/CxfConfig.java new file mode 100644 index 0000000..286dbe7 --- /dev/null +++ b/spring-boot-webservice/src/main/java/xyz/zhouzhaodong/springbootwebservice/config/CxfConfig.java @@ -0,0 +1,32 @@ +package xyz.zhouzhaodong.springbootwebservice.config; + +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 xyz.zhouzhaodong.springbootwebservice.service.CommonService; + +/** + * cxf配置类 + * @author zhouzhaodong + */ +@Configuration +public class CxfConfig { + @Autowired + private Bus bus; + + @Autowired + CommonService commonService; + + /** JAX-WS **/ + @Bean + public Endpoint endpoint() { + EndpointImpl endpoint = new EndpointImpl(bus, commonService); + //发布地址 + endpoint.publish("/CommonService"); + return endpoint; + } +} diff --git a/spring-boot-webservice/src/main/java/xyz/zhouzhaodong/springbootwebservice/service/CommonService.java b/spring-boot-webservice/src/main/java/xyz/zhouzhaodong/springbootwebservice/service/CommonService.java new file mode 100644 index 0000000..62da1d3 --- /dev/null +++ b/spring-boot-webservice/src/main/java/xyz/zhouzhaodong/springbootwebservice/service/CommonService.java @@ -0,0 +1,23 @@ +package xyz.zhouzhaodong.springbootwebservice.service; + +import javax.jws.WebMethod; +import javax.jws.WebParam; +import javax.jws.WebService; + +/** + * @author zhouzhaodong + */ +@WebService(name = "CommonService", // 暴露服务名称 + targetNamespace = "http://springbootwebservice.zhouzhaodong.xyz/"// 命名空间,一般是接口的包名倒序 +) +public interface CommonService { + + /** + * 暴露接口 + * @param name + * @return + */ + @WebMethod + String sayHello(@WebParam(name = "userName") String name); + +} diff --git a/spring-boot-webservice/src/main/java/xyz/zhouzhaodong/springbootwebservice/service/impl/CommonServiceImpl.java b/spring-boot-webservice/src/main/java/xyz/zhouzhaodong/springbootwebservice/service/impl/CommonServiceImpl.java new file mode 100644 index 0000000..a3e523f --- /dev/null +++ b/spring-boot-webservice/src/main/java/xyz/zhouzhaodong/springbootwebservice/service/impl/CommonServiceImpl.java @@ -0,0 +1,22 @@ +package xyz.zhouzhaodong.springbootwebservice.service.impl; + +import org.springframework.stereotype.Component; +import xyz.zhouzhaodong.springbootwebservice.service.CommonService; + +import javax.jws.WebService; + +/** + * @author zhouzhaodong + */ +@WebService(serviceName = "CommonService", // 与接口中指定的name一致 + targetNamespace = "http://springbootwebservice.zhouzhaodong.xyz/", // 与接口中的命名空间一致,一般是接口的包名倒 + endpointInterface = "xyz.zhouzhaodong.springbootwebservice.service.CommonService"// 接口地址 +) +@Component +public class CommonServiceImpl implements CommonService { + + @Override + public String sayHello(String name) { + return "HELLO" + name; + } +} diff --git a/spring-boot-webservice/src/main/resources/application.yml b/spring-boot-webservice/src/main/resources/application.yml new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/spring-boot-webservice/src/main/resources/application.yml @@ -0,0 +1 @@ +