ITeye博客

CXF 学习 (一)

ChineseThink · 2012-05-21 23:02 · webservice · 企业架构 · Apache · 框架 · cxf · Markdown

为了了解WebService,在网上找了组视频连续的看了几天,收获良多。 基本上对JAX-WS接口规范基础有所了解,以及Java开发Webservice的整体步骤。 视频资源地址:[url]http://www.verycd.com/topics/2917293/[/url] 确实将得挺好的,可以学习下。 看完我就想找个框架了解下,由于CXF可以与Spring无缝整合,就选择其作为入口。 下面是我的第一个实例: --服务器端 1.导入Web项目所需要的jar包 2.web.xml文件的编写 [code="java"] CXFServlet org.apache.cxf.transport.servlet.CXFServlet 0 CXFServlet /cxftest/* [/code] 3.编写接口Login接口 [code="java"]package com.example.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface Login { @WebMethod public String login(@WebParam(name="name") String name, @WebParam(name="password")String password); }[/code] 4.编写接口的实现类LoginImpl (注意Annotation的配置) [code="java"]package com.example.service.impl; import javax.jws.WebService; import com.example.service.Login; @WebService(endpointInterface="com.example.service.Login",serviceName="Login") public class LoginImpl implements Login { public String login(String name, String password) { String result = "登录CXF服务器成功。。。"; if(!"cxf".equalsIgnoreCase(name) || !"cxf".equalsIgnoreCase(password)){ return "账号密码错误。"; } return result; } } [/code] 5.创建一个Servlet实例(InitServlet)来发布接口 [code="java"]package com.example.startup; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import com.example.service.impl.LoginImpl; public class InitServlet extends HttpServlet { public void init() throws ServletException { LoginImpl loginImpl = new LoginImpl(); JaxWsServerFactoryBean jaxFactory = new JaxWsServerFactoryBean(); jaxFactory.setAddress("/Login"); jaxFactory.setServiceClass(LoginImpl.class); jaxFactory.setServiceBean(loginImpl); jaxFactory.create(); System.out.println("InitServlet..."); } }[/code] 6.InitServlet在Web.xml中的配置 [code="java"] InitServlet com.example.startup.InitServlet 1 InitServlet /init [/code] 7.在浏览器地址栏中输入http://localhost:8080/cxf_002/cxftest/Login?wsdl 出现wsdl的xml文件信息。 --客户端测试代码 1.在服务开启的情况下使用wsimport工具根据生成客户端代码 (wsimport -d d:/service/04 -keep -verbose http://localhost:8080/cxf_002/cxftest/Login?wsdl) 或者使用CXF 自带的工具wsdl2java (wsdl2java -d:/service/04 -client http://localhost:8080/cxf_002/cxftest/Login?wsdl) 2.将impl包中的Login.java拷贝至测试客户端中 3.使用Main方法测试接口的实现 [code="java"]package com.example.start; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.example.service.impl.Login; public class LoginTest { public static void main(String[] args) { JaxWsProxyFactoryBean jaxFactory = new JaxWsProxyFactoryBean(); jaxFactory.setAddress("http://localhost:8080/cxf_002/cxftest/Login?wsdl"); jaxFactory.setServiceClass(Login.class); Login login = (Login)jaxFactory.create(); String result = login.login("cxf", "cxf"); System.out.println("结果:"+result); } }[/code] 下面附上示例源码(注意需要引入CXF的jar文件):

评论(1)

  1. #1 楼 · wangyudong · 2017-11-27 23:19

    由CXF实现的微服务需要有比较好的工具去测试RESTful API,很多REST Client是不支持自动化测试RESTful API,也不支持自动生成API文档. 之前习惯用一款名字为 WisdomTool REST Client,支持自动化测试RESTful API,输出精美的测试报告,并且自动生成精美的RESTful API文档。 轻量级的工具,功能却很精悍哦!

    https://github.com/wisdomtool/rest-client

    Most of REST Client tools do not support automated testing.

    Once used a tool called WisdomTool REST Client supports automated testing, output exquisite report, and automatically generating RESTful API document.

    Lightweight tool with very powerful features!

    https://github.com/wisdomtool/rest-client

评论功能维护中,当前仅展示历史评论。