Spring Boot+CXF搭建WebService服务参考资料
pom.xml文件引入包:
<!--WerbService CXF依赖--> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.12</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.12</version> </dependency>
模拟代码:
package webServiceDemo; import java.io.Serializable; /** * TODO * 实体类 */ public class User implements Serializable { private String name; private String age; private String xb; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getXb() { return xb; } public void setXb(String xb) { this.xb = xb; } public User() { } public User(String name, String age, String xb) { this.name = name; this.age = age; this.xb = xb; } }
package webServiceDemo; import javax.jws.WebMethod; import javax.jws.WebService; /** * TODO * webService接口 */ @WebService public interface UserWebService { @WebMethod User getUserByName(String name); }
package webServiceDemo; import javax.jws.WebService; import java.util.ArrayList; import java.util.List; /** * TODO * webService接口实现类
* endpointInterface为UserWebService所在项目地址(包含接口名),targetNamespace为UserWebService所在项目地址倒序(不含接口名) */ @WebService(targetNamespace = "http://webServiceDemo/", endpointInterface = "webServiceDemo.UserWebService") public class UserWebServiceImpl implements UserWebService { @Override public User getUserByName(String name) { List<User> userList = new ArrayList<>(); User user1 = new User("张三", "21", "男"); userList.add(user1); User user2 = new User("李四", "19", "女"); userList.add(user2); User needUser = new User(); for (User user : userList) { if (!user.getName().equals(name)) { continue; } needUser = user; } return needUser; } }
package webServiceDemo; 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; /** * TODO * WebService服务发布配置 */ @Configuration public class WebServiceConfig { @Bean public ServletRegistrationBean disServlet(){ return new ServletRegistrationBean(new CXFServlet(), "/service/*"); //发布服务名称 } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public UserWebService userWebService() { return new UserWebServiceImpl(); } /* WebService服务发布 */ @Bean public Endpoint endpoint() { //绑定要发布的服务 EndpointImpl endpoint = new EndpointImpl(springBus(), userWebService()); endpoint.publish("/userWebService"); //显示要发布的名称 return endpoint; } }
然后启动项目,创建的WebService服务会随着项目启动发布,启动成功后,根据发布配置浏览器访问:
http://localhost:8080/service/userWebService?wsdl
构建服务端参考资料:
https://www.cnblogs.com/xibei666/p/8970549.html;
https://blog.csdn.net/cs_hnu_scw/article/details/80181762;
https://www.imooc.com/article/25537?block_id=tuijian_wz;
遇到的问题及解决办法:
https://www.cnblogs.com/Big-Boss/p/11912679.html;