第四周 - CXF Restful Service 基础实验

    xiaoxiao2023-03-24  4

    下载程序包 http://cxf.apache.org/download.html 选择 跳转页面后下载: 解压。samples/jax_rs/basic 是我们要的案例。用 eclipse 导入项目 File -> Imports -> Existing Maven Projects 找到samples/jax_rs/basic 目录导入

    运行项目

    install:安装,你会看到制品放入本地仓库 -Pserver: 运行服务器,程序会自动启动 jetty -Pclient: 运行客户端程序

    pom.xml 右键 - Run As - 2 Maven build 输入install: 控制台部分输出: 运行-Pserver(方法同上) 控制台部分输出: 在浏览器中输入URL http://localhost:9000/customerservice/customers/123 得到如下结果: 对于这个返回结果README.txt中有如下说明:

    @GET //#@GET HTTP协议方法,常见的有 GET,PUT,POST,DELETE 等 @Path("/customers/{id}/") //#@Path(“/customers/{id}/”) 项目根访问该方法的 URI 路径,其中 {ID} 是一个变量模板, @PathParam(“id”) 可以获取该变量。 public Customer getCustomer(@PathParam("id") String id) { System.out.println("----invoking getCustomer, Customer id is: " + id); long idNumber = Long.parseLong(id); Customer c = customers.get(idNumber); return c; }

    实验:修改@Path 为 @Path(“/myservice/”),用 curl -v 给出结果 打开customerServer.java: 修改为 重新执行maven命令install和-Pserver -Pserver时会报错: 是因为9000端口被占用。退出ecplipse再重新打开执行-Pserver 。 启动成功: 访问http://localhost:9000/myservice/customers/123 可以正常访问得到如下结果: 用curl -v指令:

    转载请注明原文地址: https://ju.6miu.com/read-1200097.html
    最新回复(0)