700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > SpringBoot整合CXF框架开发

SpringBoot整合CXF框架开发

时间:2019-09-23 01:58:24

相关推荐

SpringBoot整合CXF框架开发

在开发过程中遇到了一些使用webservice开发的需求,后查阅资料学习,可上手开发。在学习过程中实现了个小demo,为了养成良好的总结习惯(我还没这”坏习惯“),特意写了个小呆萌,记录一下搭建过程。

首先导入必需jar(pom.xml)

<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><!--<version>2.1.2.RELEASE</version>--><version>2.0.1.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.demo</groupId><artifactId>springboot-cxf-demo</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-cxf-demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!-- CXF webservice --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId><version>3.2.4</version></dependency><!-- CXF webservice --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

新建测试服务

UserService.java

package com.demo.springbootcxfdemo.service;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebService;//指定webservice命名空间@WebService(targetNamespace = "http://service.")public interface UserService {@WebMethod(operationName = "getHello")//在wsdl文档中显示的方法名,可不指定默认与方法相同;@WebMethod可不指定String getHello(@WebParam(name = "name") String name);}

UserServiceImpl.java

package com.demo.springbootcxfdemo.service.impl;import com.demo.springbootcxfdemo.service.UserService;import org.ponent;/*** @Author Guixing* @Date /1/17 14:00* @Description*/@Componentpublic class UserServiceImpl implements UserService {@Overridepublic String getHello(String name) {return "hello "+name;}}

配置服务访问路径并发布服务

package com.demo.springbootcxfdemo.config;import com.demo.springbootcxfdemo.service.UserService;import org.apache.cxf.Bus;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;/*** @Author Guixing* @Date /1/17 14:01* @Description*/@Configurationpublic class CXFConfig {@Autowiredprivate Bus bus;@Autowiredprivate UserService userService;/*** 此方法被注释后:wsdl访问地址为http://127.0.0.1:8080/services/user?wsdl* 去掉注释后:wsdl访问地址为:http://127.0.0.1:8080/soap/user?wsdl*/@Beanpublic ServletRegistrationBean dispatcherServlet(){return new ServletRegistrationBean(new CXFServlet(),"/soap/*");}/*** 发布服务* 指定访问url* @return*/@Beanpublic Endpoint userEndpoint(){EndpointImpl endpoint = new EndpointImpl(bus,userService);endpoint.publish("/user");return endpoint;}}

测试

浏览器中查看

可以在浏览器中输入http://localhost:8080/soap/user?wsdl来查看

红线圈出为可查看详情的url:http://localhost:8080/soap/user?wsdl=UserService.wsdl

java客户端访问

CXFClient.java

package com.demo.springbootcxfdemo.client;import com.demo.springbootcxfdemo.service.UserService;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;/*** @Author Guixing* @Date /1/17 14:53* @Description*/public class CXFClient {public static void main(String[] args) {try {// 接口地址String address = "http://127.0.0.1:8080/soap/user?wsdl";// 代理工厂JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();// 设置代理地址jaxWsProxyFactoryBean.setAddress(address);// 设置接口类型jaxWsProxyFactoryBean.setServiceClass(UserService.class);// 创建一个代理接口实现UserService us = (UserService) jaxWsProxyFactoryBean.create();// 数据准备String name = "zhang";// 调用代理接口的方法调用并返回结果String result = us.getHello(name);System.out.println("返回结果:" + result);} catch (Exception e) {e.printStackTrace();}}}

postman测试

请求中的name标签即为@WebParam中name指定的名称,下面为后端返回数据。

写在最后

到这里一个简单的springboot整合cxf框架开发webservice服务的demo就开发完了。

源码地址:/zhangguixing/springboot-demo

学习使我快乐!?

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。