| pom.xml | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| src/main/java/com/zy/common/webservice/config/WebServiceConfig.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| src/main/java/com/zy/common/webservice/mock/DemoMock.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| src/main/java/com/zy/common/webservice/service/WebServiceDemoService.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| src/main/java/com/zy/common/webservice/service/WebServiceDemoServiceImpl.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
pom.xml
@@ -97,6 +97,22 @@ <version>1.16.22</version> <scope>provided</scope> </dependency> <!-- WebService 服务端 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.3.4</version> </dependency> <!-- WebService 客户端 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.3.4</version> </dependency> </dependencies> <build> src/main/java/com/zy/common/webservice/config/WebServiceConfig.java
New file @@ -0,0 +1,50 @@ package com.zy.common.webservice.config; import com.zy.common.webservice.service.WebServiceDemoService; import org.apache.cxf.Bus; import org.apache.cxf.bus.spring.SpringBus; 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; /** * Created by vincent on 2021/7/12 */ @Configuration public class WebServiceConfig { @Autowired private WebServiceDemoService webServiceDemoService; /** * 注入servlet bean name不能dispatcherServlet 否则会覆盖dispatcherServlet * @return */ @Bean(name = "cxfServlet") public ServletRegistrationBean cxfServlet() { return new ServletRegistrationBean(new CXFServlet(),"/webservice/*"); } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } /** * 注册WebServiceDemoService接口到webservice服务 * @return */ @Bean(name = "WebServiceDemoEndpoint") public Endpoint sweptPayEndpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), webServiceDemoService); endpoint.publish("/webservice"); return endpoint; } } src/main/java/com/zy/common/webservice/mock/DemoMock.java
New file @@ -0,0 +1,27 @@ package com.zy.common.webservice.mock; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; /** * Created by vincent on 2021/7/12 */ public class DemoMock { public static void main(String[] args) { JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient("http://localhost:8081/gdwms/webservice/webservice?wsdl"); Object[] objects = new Object[0]; ObjectMapper mapper = new ObjectMapper(); try { // invoke("方法名",参数1,参数2,参数3....); objects = client.invoke("hello", "vincent"); System.out.println(mapper.writeValueAsString(objects[0])); } catch (java.lang.Exception e) { e.printStackTrace(); } } } src/main/java/com/zy/common/webservice/service/WebServiceDemoService.java
New file @@ -0,0 +1,15 @@ package com.zy.common.webservice.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; @WebService(name = "WebServiceDemoService", // 暴露服务名称 targetNamespace = "http://service.webservice.common.zy.com"// 命名空间,一般是接口的包名倒序 ) public interface WebServiceDemoService { @WebMethod String hello(@WebParam(name = "name")String name); } src/main/java/com/zy/common/webservice/service/WebServiceDemoServiceImpl.java
New file @@ -0,0 +1,22 @@ package com.zy.common.webservice.service; import org.springframework.stereotype.Service; import javax.jws.WebService; /** * Created by vincent on 2021/7/12 */ @Service @WebService(serviceName = "WebServiceDemoService", // 与接口中指定的name一致 targetNamespace = "http://service.webservice.common.zy.com", // 与接口中的命名空间一致,一般是接口的包名倒 endpointInterface = "com.zy.common.webservice.service.WebServiceDemoService" // 接口地址 ) public class WebServiceDemoServiceImpl implements WebServiceDemoService { @Override public String hello(String name) { return "ok" + name; } }