1.创建maven项目 加入pom依赖

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>apache-cxf</artifactId>
<version>3.0.2</version>
<type>pom</type>
</dependency>

2.编写实体类,接口类与接口实现

User.java

package com.itstudy.domain;

import javax.xml.bind.annotation.*;

@XmlRootElement(name = "User")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "User")
public class User {
@XmlElement(nillable = true)
private Long id; @XmlElement(nillable = true)
private String name; @XmlElement(nillable = true)
private int age; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
}

HelloService.java

package com.itstudy.service;

import com.itstudy.domain.User;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import java.util.List; @WebService
public interface HelloService { @WebMethod
public String say(@WebParam(name="name")String name); @WebMethod
public String sayHello(@WebParam(name="user") User user); @WebMethod
public List<User> getList(Long id);
}

HelloServiceImpl.java

package com.itstudy.service.impl;

import com.itstudy.domain.User;
import com.itstudy.service.HelloService; import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import java.util.ArrayList;
import java.util.List; @WebService
public class HelloServiceImpl implements HelloService {
@WebMethod
public String say(@WebParam(name = "name") String name) {
return name + ",您好!";
} @WebMethod
public String sayHello(@WebParam(name = "user") User user) {
return user.getName() + ",您好!";
} @WebMethod
public List<User> getList(Long id) {
List<User> list = new ArrayList<User>();
User user = new User();
Long sid = 1L;
user.setId(sid);
user.setName("张三" + sid);
list.add(user); user = new User();
sid = 2L;
user.setId(sid);
user.setName("张三" + sid);
list.add(user); return list;
}
}

3.启动 服务

package com.itstudy;

import com.itstudy.service.HelloService;
import com.itstudy.service.impl.HelloServiceImpl;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean; public class App {
public static void main(String[] args) {
//启动方式1 此方式接口实现必须添加注解 @WebService @WebMethod等
// System.out.println( "Hello World!" );
// HelloServiceImpl implementor = new HelloServiceImpl();
// String address = "http://localhost:8080/ws/" + HelloService.class.getSimpleName();
// Endpoint.publish(address, implementor); //启动方式2 此方式接口实现不需要添加注解
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(HelloService.class);
// 发布接口  
factory.setAddress("http://localhost:8080/ws/" + HelloService.class.getSimpleName());
factory.setServiceBean(new HelloServiceImpl());
factory.create();
      
     //发布多个接口,多定义几个factory即可 }
}

4.动态调用webservice

package com.itstudy;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; public class HelloWorldClient {
public static void main(String[] argv) { JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client =
dcf.createClient("http://localhost:8080/ws/HelloService?wsdl"); //sayHello为接口中定义的方法名称张三为传递的参数返回一个Object数组
Object[] objects = new Object[0];
try {
objects = client.invoke("getList", 1L);
} catch (Exception e) {
e.printStackTrace();
} //输出调用结果
System.out.println(objects[0]); }
}

总结
1.两种创建方式
  1.1 JaxWsServerFactoryBean启动(接口实现不需要相关注解)
  1.2 Endpoint启动 (接口实现与需要相关注解)
2.三种调用方式
  2.1动态代理
  2.2拿到现有接口与类
  2.3生成相关代理类

参考文档

cxf 发布多个接口

https://blog.csdn.net/qq_21399933/article/details/78818240

cxf 三种发布方式与4种调用方式
https://blog.csdn.net/u011498933/article/details/75355338

cxf 客户端调用2
https://blog.csdn.net/z69183787/article/details/53488887
cxf 客户端调用3
https://my.oschina.net/nba/blog/482117

cxf 安全认证1
https://blog.csdn.net/rangqiwei/article/details/19282271

cxf 安全认证2
https://blog.csdn.net/weixin_41138656/article/details/79393366

cxf实战 创建webservice 与创建restfulapi
https://blog.csdn.net/kongxx/article/details/7534035

java webservice - cxf使用总结 一的更多相关文章

  1. Java WebService 教程系列之 Spring 整合 CXF

    Java WebService 教程系列之 Spring 整合 CXF 一.引入 jar 包 <dependency> <groupId>org.apache.cxf</ ...

  2. 转-JAVA webservice之CXF 范例--http://cxshun.iteye.com/blog/1275408

    JAVA webservice之CXF 博客分类: j2ee相关 昨天我们一起学习了一下xfire,今天我们来看一下CXF,为什么学完那个接着学这个呢.因为CXF是在xfire的基础上实现 的,所以我 ...

  3. Java WebService学习笔记 - Axis(一)

    WebService 简介 实际开发中,很多系统都是基于历史遗留系统进行开发,有时,这些系统基于不同的语言,如C,C++,C#,java,PHP等等.为了实现历史系统的再利用,或向外部程序暴露调用接口 ...

  4. Java WebService 开发简单实例

    Web Service 是一种新的web应用程序分支,他们是自包含.自描述.模块化的应用,可以发布.定位.通过web调用.Web Service可以执行从简单的请求到复杂商务处理的任何功能.一旦部署以 ...

  5. paip.myeclipse7 java webservice 最佳实践o228

    paip.myeclipse7  java webservice 最佳实践o228 java的ws实现方案:jax-ws>>xfire ws的测试工具  webservice测试调用工具W ...

  6. MAXIMO系统 java webservice 中PDA移动应用系统开发

    MAXIMO系统 java webservice 中PDA移动应用系统开发  平时经常用的wince PDA手持设备调用c#写的webservice, 当然PDA也可以调用java webservic ...

  7. c#调用带有安全认证的java webservice

    最近使用c#调用另外一个同事写的java webservice耽误了很多时间,网上资料不太完整,走了很多弯路,希望对大家有帮助. 基本思路是1.拼装soap使用http post ,主要将验证身份信息 ...

  8. 为什么C#动态调用Java的cxf多了bool型参数

    最近的一个项目需要C#调用Java的cxf发布的接口,接口参数文档只给我的是两个long型,但是通过我动态加载发现,参数是四个. 比如接口文档给的接口是 TestFunc(long, long); 而 ...

  9. 主题:Java WebService 简单实例

    链接地址:主题:Java WebService 简单实例    http://www.iteye.com/topic/1135747 前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要 ...

随机推荐

  1. php内置函数分析array_diff()

    PHP_FUNCTION(array_diff) { zval *args; int argc, i; uint32_t num; HashTable exclude; zval *value; ze ...

  2. 什么是LMDB闪电记忆映射数据库

    LightningMemory-MappedDatabase(LMDB)是一个软件库,它以键值存储的形式提供高性能的嵌入式事务数据库.LMDB是用C语言编写的,具有多种编程语言的API绑定.LMDB将 ...

  3. kettle imestamp : Unable to get timestamp from resultset at index 22

    在做ETL的时候,连接MySQL读取含有timestamp类型的表,出现如下错误: 经Google,据说是MySQL自身的问题.解决方法也很简单,在Spoon的数据库连接中,打开选项,加入一行命令参数 ...

  4. java:Set对象TreeSet有序子类,HashSet无序子类,重复对象二

    TreeSet有序子类; HashSet无序子类 重复重复元素,Object对象是通过equals和hashCode来进行过滤的. 如果将上一篇提到中的例子中的TreeSet,换成HashSet,那么 ...

  5. sqlserver数据库脱机和分离的区别

    脱机和分离的区别: 分离和脱机都可以使数据库不能再被使用,但是分离后需要附加才能使用,而脱机后只需联机就可以用了. 附加数据库报错: 无法打开物理文件 XXX.mdf".操作系统错误 5:& ...

  6. 【leetcode】1155. Number of Dice Rolls With Target Sum

    题目如下: You have d dice, and each die has f faces numbered 1, 2, ..., f. Return the number of possible ...

  7. koa2基础

    1.安装脚手架 npm install -g koa-generator 2.创建目录 koa2 -e koa2-learn 3.cd koa2-learn 4.npm install 5.SET D ...

  8. #431 Div2 Problem B Tell Your World (鸽巢原理 && 思维)

    链接 : http://codeforces.com/contest/849/problem/B 题意 : 给出 n 个在直角坐标系上的点,每个点的横坐标的值对应给出的顺序序数,比如 1 2 4 3 ...

  9. linux ubantu php composer安装

    root@iZwz93telmwbh624e5zetqZ:~# php -v PHP 5.6.40-14+ubuntu16.04.1+deb.sury.org+1 (cli) Copyright (c ...

  10. [BZOJ1478&1488&1815][SGU282]Isomorphism:Polya定理

    分析 三倍经验题,本文以[BZOJ1478][SGU282]Isomorphism为例展开叙述,主体思路与另外两题大(wan)致(quan)相(yi)同(zhi). 这可能是博主目前写过最长也是最认真 ...