SpringBoot WebService 源代码:https://gitee.com/VipSoft/VipWebService
SpringBoot 版本 <version>2.3.0.RELEASE</version> <cxf.version>3.3.1</cxf.version> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-core</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version> </dependency>
package com.vipsoft.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; @WebService(name = "DemoWebService", // 暴露服务名称,不指定就会使用类名 targetNamespace = "http://service.his.vipsoft.com"// 与接口中的命名空间一致,一般是接口的包名倒 不加这个,SOAPUI 获取不到地址 ) public interface IDemoWebService { @WebMethod(action = "http://ws.vipsoft.com/sayHello") //有些集成平台,如果不加 action 会调不成功 public String sayHello(@WebParam(name = "arg") String arg); }
package com.vipsoft.service.impl; import com.vipsoft.service.IDemoWebService; import org.springframework.stereotype.Component; import javax.jws.WebService; import java.util.Date; @Servicepublic class DemoWebServiceImpl implements IDemoWebService { @Override public String sayHello(String arg) { return arg + ",现在时间:" + "(" + new Date() + ")"; } }
package com.vipsoft.web.config; import com.vipsoft.service.IDemoWebService; import com.vipsoft.service.impl.DemoWebServiceImpl; 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.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.xml.ws.Endpoint; @Configuration public class CxfConfig { @SuppressWarnings("all") @Bean(name = "cxfServlet") //不加可能会报错 public ServletRegistrationBean cxfServlet() { return new ServletRegistrationBean(new CXFServlet(), "/ws/*"); } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public IDemoWebService demoService() { return new DemoWebServiceImpl(); } @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), demoService()); //绑定要发布的服务 endpoint.publish("/api"); // 暴露webService api,用在资源访问 return endpoint; } }
Error loading [http://localhost:9090/ws/api?wsdl]: java.lang.Exception: Load of url [http://localhost:9090/ws/api?wsdl] was aborted
WebService 不能使用 localhost 访问,可以通过 ipconfig 查看本机IP,使用IP进行访问
http://192.168.1.216:9090/ws/api?wsdl