eclipse开发基于CXF的Web Service项目

  1. 开发环境准备:
    1. Tomcat 8
    2. Eclipse 4.5.2
    3. Apache-cxf jar包(用于在eclipse 中配置CXF Runtime),这里我使用的版本是3.1.10
  2. 新建一个动态web工程server(作为Server
  3. 写入代码:
    1. dto

package com.hand.cxf_server.dto;

 

import javax.jws.WebService;

 

@WebService

public class Order {

 

private int id;

private String name;

private double price;

 

public int getId() {

return id;

}

 

public void setId(int id) {

this.id = id;

}

 

public String getName() {

return name;

}

 

public void setName(String name) {

this.name = name;

}

 

public double getPrice() {

return price;

}

 

public void setPrice(double price) {

this.price = price;

}

 

@Override

public String toString() {

return "OrderWS [id=" + id + ", name=" + name + ", price=" + price + "]";

}

 

public Order(int id, String name, double price) {

super();

this.id = id;

this.name = name;

this.price = price;

}

 

}

  1. Interface接口OrderWS

package com.hand.cxf_server.ws.service;

 

import javax.jws.WebMethod;

import javax.jws.WebService;

 

import com.hand.cxf_server.dto.Order;

 

@WebService

public interface OrderWS {

 

@WebMethod

public Order getOrderById(int id);

}

  1. 实现类

package com.hand.cxf_server.ws.service.impl;

 

import javax.jws.WebService;

 

import com.hand.cxf_server.dto.Order;

import com.hand.cxf_server.ws.service.OrderWS;

 

public class OrderWSImpl implements OrderWS {

 

public Order getOrderById(int id) {

System.out.println("Server getOrderById(): "+id);

return new Order(id, "飞机", 999999999);

}

 

}

  1. OrderWS 右键->Web Service ->Create Web Service
  2.  a. 修改服务器和Service接口

  1. 按步骤next,直至创建完毕
  2. 启动tomcat服务器,发布服务
  3. 如果遇到错误prefix wsdp is not bound to a namespace
    1. 解决办法:删除cxf库中前缀为cxf-services的jar
  4. 如果没有错误,在浏览器中输入http://localhost:8080/server/services/orderws?wsdl,检查是否发布成功

 

  1. 新建动态web工程(Client)
  2. New Dynamic Web Project Dynamic Web Project Create a standalone Dynamic Web project or add it to a new or existing Enterprise Application. O Project name: Client Project location use default location Location: Ta rget runtime Apache Tomcat v8.O Dynamic web module version Config u ra tion CXF Web Services Project v2.5 Configures a Dynamic Web Project EAR membership Add project to an EAR EAR project name: ClientEAR Working sets Add project to working sets Wgrking sets: CXF using Web M v2.5 and Java v5.O N ext Finish New Ru nti mem M odi6/m New erojectm Selectm Cancel
  3. 右键New->Other->Web Service Client

New Select a wizard Access an existing XML web service Wiza rd s: pe filter text SQL Development Tasks Tern user Assista nce Web Web Services k] Ant Files unit Test UDDI Web Service Web Service Client Next >  

 

Web Service Client Web Services Select a service definition and move the slider to set the level of client generation. Service definition: 8 rowsem Client type: Java Proxy üer,loy c!ient Monitor the Web service o Config u ra tion: Server runtime: Tomcat v8.O Server Web service runtime: A ache CXF 2.x Client proiect: Client Finish Ca ncel
  1. 新建cxf-client.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:jaxws="http://cxf.apache.org/jaxws"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

                       http://www.springframework.org/schema/beans/spring-beans.xsd

                       http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

 

<jaxws:client id="orderClient" serviceClass="com.hand.cxf_server.ws.service.OrderWS" address="http://localhost:8080/server/services/orderws"></jaxws:client>

</beans>

  1. 新建测试类Main

package com.hand.cxf_server.ws.service.test;

 

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import com.hand.cxf_server.ws.service.Order;

import com.hand.cxf_server.ws.service.OrderWS;

 

public class Main {

public static void main(String[] args) {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("cxf-client.xml");

OrderWS orderWS = (OrderWS) context.getBean("orderClient");

Order order = orderWS.getOrderById(33);

System.out.println(order);

}

}

  1. 运行查看结果
  2. 备注:
    1. 如果使用maven部署项目,则maven依赖如下

<dependency>

<groupId>org.apache.cxf</groupId>

<artifactId>cxf-rt-transports-http</artifactId>

<version>${cxf.version}</version>

</dependency>

<dependency>

<groupId>org.apache.cxf</groupId>

<artifactId>cxf-rt-bindings-soap</artifactId>

<version>${cxf.version}</version>

</dependency>

<dependency>

<groupId>org.apache.cxf</groupId>

<artifactId>cxf-rt-frontend-jaxws</artifactId>

<version>${cxf.version}</version>

</dependency>

 

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>${spring.version}</version>

</dependency>

  1. 跨域问题:可以使用HttpURLConnection请求webservice
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值