SpringBoot整合CXF实例:
服务端构建
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.2.5</version> </dependency>
/** * 作者信息实体 * @author oKong * */ @Data @Builder @AllArgsConstructor @NoArgsConstructor public class AuthorDto { String name; List<String> hobby; String birthday; String description; Sex sex; }
/** * 性别枚举类 * @author oKong * */ public enum Sex { MALE("male"), FEMALE("female"); String value; Sex(String value) { this.value = value; } public String value() { return value; } public static Sex fromValue(String v) { for (Sex c : Sex.values()) { if (c.value.equals(v)) { return c; } } throw new IllegalArgumentException(v); } }
/** * 创建服务接口 * @author oKong * */ @WebService(targetNamespace = WsConst.NAMESPACE_URI ,name = "authorPortType") public interface AuthorService { /** * 根据名称获取作者信息 * @author 作者:oKong */ @WebMethod(operationName="getAuthorByName") AuthorDto getAuthor(@WebParam(name = "authorName") String name); /** * 获取作者列表信息 * @author oKong */ @WebMethod List<AuthorDto> getAuthorList(); /** * 返回字符串测试 * @author oKong */ String getAuthorString(@WebParam(name = "authorName")String name); }
@WebService( targetNamespace = WsConst.NAMESPACE_URI, //wsdl命名空间 name = "authorPortType", //portType名称 客户端生成代码时 为接口名称 serviceName = "authorService", //服务name名称 portName = "authorPortName", //port名称 endpointInterface = "cn.lqdev.learning.springboot.cxf.service.AuthorService")//指定发布webservcie的接口类,此类也需要接入@WebService注解 public class AuthorServiceImpl implements AuthorService{ @Override public AuthorDto getAuthor(String name) { AuthorDto author = new AuthorDto(); author.setBirthday("1990-01-23"); author.setName("姓名:" + name); author.setSex(Sex.MALE); author.setHobby(Arrays.asList("电影","旅游")); author.setDescription("描述:一枚趔趄的猿。现在时间:" + new Date().getTime()); return author; } @Override public List<AuthorDto> getAuthorList() { List<AuthorDto> resultList = new ArrayList<>(); AuthorDto author = new AuthorDto(); author.setBirthday("1990-01-23"); author.setName("姓名:oKong"); author.setSex(Sex.MALE); author.setHobby(Arrays.asList("电影","旅游")); author.setDescription("描述:一枚趔趄的猿。现在时间:" + new Date().getTime()); resultList.add(author); resultList.add(author); return resultList; } @Override public String getAuthorString(String name) { AuthorDto author = getAuthor(name); return author.toString(); } }
@WebService( targetNamespace = WsConst.NAMESPACE_URI, //wsdl命名空间 name = "authorPortType", //portType名称 客户端生成代码时 为接口名称 serviceName = "authorService", //服务name名称 portName = "authorPortName", //port名称 endpointInterface = "cn.lqdev.learning.springboot.cxf.service.AuthorService")//指定发布webservcie的接口类,此类也需要接入@WebService注解
/** * 常量类 * @author oKong * */ public class WsConst { public static final String NAMESPACE_URI = "http://www.lqdev.cn/webservice"; }
/** * cxf配置类 * @author oKong * */ @Configuration public class CxfWebServiceConfig { //这里需要注意 由于springmvc 的核心类 为DispatcherServlet //此处若不重命名此bean的话 原本的mvc就被覆盖了。可查看配置类:DispatcherServletAutoConfiguration //一种方法是修改方法名称 或者指定bean名称 //这里需要注意 若beanName命名不是 cxfServletRegistration 时,会创建两个CXFServlet的。 //具体可查看下自动配置类:Declaration org.apache.cxf.spring.boot.autoconfigure.CxfAutoConfiguration //也可以不设置此bean 直接通过配置项 cxf.path 来修改访问路径的 @Bean("cxfServletRegistration") public ServletRegistrationBean dispatcherServlet() { //注册servlet 拦截/ws 开头的请求 不设置 默认为:/services/* return new ServletRegistrationBean(new CXFServlet(), "/ws/*"); } /** * 申明业务处理类 当然也可以直接 在实现类上标注 @Service * @author oKong */ @Bean public AuthorService authorService() { return new AuthorServiceImpl(); } /* * 非必要项 */ @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { SpringBus springBus = new SpringBus(); return springBus; } /* * 发布endpoint */ @Bean public Endpoint endpoint(AuthorService authorService) { EndpointImpl endpoint = new EndpointImpl(springBus(), authorService); endpoint.publish("/author");//发布地址 return endpoint; } }
访问:http://127.0.0.1:8080/ws/author?wsdl ,验证是否发布成功。
客户端调用:
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.2.5</version> </dependency>
右键项目文件---webservice--
WsConst.java:
/** * 常量类 * @author oKong * */ public class WsConst { public static final String NAMESPACE_URI = "http://www.lqdev.cn/webservice"; public static final String SERVICE_ADDRESS= "http://127.0.0.1:8080/ws/author?wsdl"; //为服务端地址 }
/** * 配置类 * * @author oKong * */ @Configuration public class CxfClientConfig { /** * 以接口代理方式进行调用 AuthorPortType接口 */ @Bean("cxfProxy") public AuthorPortType createAuthorPortTypeProxy() { JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean(); jaxWsProxyFactoryBean.setServiceClass(AuthorPortType.class); jaxWsProxyFactoryBean.setAddress(WsConst.SERVICE_ADDRESS);//服务地址:http://127.0.0.1:8080/ws/autho return (AuthorPortType) jaxWsProxyFactoryBean.create(); } /* * 采用动态工厂方式 不需要指定服务接口 */ @Bean public Client createDynamicClient() { JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient(WsConst.SERVICE_ADDRESS); return client; } }
/** * 调用示例 * @author oKong * */ @RestController @RequestMapping("/cxf") public class DemoController { @Autowired Client client; @Autowired @Qualifier("cxfProxy") AuthorPortType authorPort; @GetMapping("/getauthorstring") public String getAuthorString(String authorName) { return authorPort.getAuthorString(authorName); } @GetMapping("/getauthor") public AuthorDto getAuthor(String authorName) { return authorPort.getAuthorByName(authorName); } @GetMapping("/getauthorlist") public List<AuthorDto> getAuthorList() { return authorPort.getAuthorList(); } @GetMapping("/dynamic/{operation}") public Object getAuthorStringByDynamic(@PathVariable("operation")String operationName, String authorName) throws Exception { //这里就简单的判断了 Object[] objects = null; // client.getEndpoint().getBinding().getBindingInfo().getOperations() if ("getAuthorList".equalsIgnoreCase(operationName)) { objects = client.invoke(operationName); } else if ("getAuthorString".equalsIgnoreCase(operationName)) { objects = client.invoke(operationName, authorName); } else if ("getAuthorByName".equalsIgnoreCase(operationName)) { objects = client.invoke(operationName, authorName); } else { throw new RuntimeException("无效的调用方法"); } return objects != null && objects.length > 0 ? objects[0] : "返回异常"; } }
端口号配置:
server.port=8090
启动应用,依次访问。查看是否调用成功。
http://127.0.0.1:8090/cxf/getauthorstring?authorName=oKong
//为客户端的地址
参考文献:https://blog.csdn.net/xie19900123/article/details/83986482