java webservice - cxf使用总结 一
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使用总结 一的更多相关文章
- Java WebService 教程系列之 Spring 整合 CXF
Java WebService 教程系列之 Spring 整合 CXF 一.引入 jar 包 <dependency> <groupId>org.apache.cxf</ ...
- 转-JAVA webservice之CXF 范例--http://cxshun.iteye.com/blog/1275408
JAVA webservice之CXF 博客分类: j2ee相关 昨天我们一起学习了一下xfire,今天我们来看一下CXF,为什么学完那个接着学这个呢.因为CXF是在xfire的基础上实现 的,所以我 ...
- Java WebService学习笔记 - Axis(一)
WebService 简介 实际开发中,很多系统都是基于历史遗留系统进行开发,有时,这些系统基于不同的语言,如C,C++,C#,java,PHP等等.为了实现历史系统的再利用,或向外部程序暴露调用接口 ...
- Java WebService 开发简单实例
Web Service 是一种新的web应用程序分支,他们是自包含.自描述.模块化的应用,可以发布.定位.通过web调用.Web Service可以执行从简单的请求到复杂商务处理的任何功能.一旦部署以 ...
- paip.myeclipse7 java webservice 最佳实践o228
paip.myeclipse7 java webservice 最佳实践o228 java的ws实现方案:jax-ws>>xfire ws的测试工具 webservice测试调用工具W ...
- MAXIMO系统 java webservice 中PDA移动应用系统开发
MAXIMO系统 java webservice 中PDA移动应用系统开发 平时经常用的wince PDA手持设备调用c#写的webservice, 当然PDA也可以调用java webservic ...
- c#调用带有安全认证的java webservice
最近使用c#调用另外一个同事写的java webservice耽误了很多时间,网上资料不太完整,走了很多弯路,希望对大家有帮助. 基本思路是1.拼装soap使用http post ,主要将验证身份信息 ...
- 为什么C#动态调用Java的cxf多了bool型参数
最近的一个项目需要C#调用Java的cxf发布的接口,接口参数文档只给我的是两个long型,但是通过我动态加载发现,参数是四个. 比如接口文档给的接口是 TestFunc(long, long); 而 ...
- 主题:Java WebService 简单实例
链接地址:主题:Java WebService 简单实例 http://www.iteye.com/topic/1135747 前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要 ...
随机推荐
- POJ-3436-ACM Computer Factory(最大流, 输出路径)
链接: https://vjudge.net/problem/POJ-3436#author=0 题意: 为了追求ACM比赛的公平性,所有用作ACM比赛的电脑性能是一样的,而ACM董事会专门有一条生产 ...
- Oracle12c RAC RMAN异机恢复
######################################################## #编辑pfile文件initspdb.ora vi /oracle/app/oracl ...
- Hybris commerce产品主数据的搜索API,批量返回若干主数据的值
新建一个产品,identifier设置为i042416-1,创建之后立即能够在Backoffice里搜索出来: 等到Storefront的indexing做完之后,前台通过关键字i042416也能将这 ...
- JSP Cookies处理
JSP Cookies处理 Cookies是存储在客户机的文本文件,它们保存了大量轨迹信息.在servlet技术基础上,JSP显然能够提供对HTTP cookies的支持. 通常有三个步骤来识别回头客 ...
- IO重定向与管道
一.三种IO设备 程序:数据+指令 或 数据结构+算法 程序必须能够读入输入然后经过加工来产生结果,其接受的输入可以是变量.数组.列表.文件等等,生产出来的结果可以使变量.数组.列表.文件等等.即: ...
- AT2370 Piling Up
https://www.luogu.org/jump/atcoder/2370 题解 答案不是\(2^{2m}\)因为每轮的第一次取球可能会不够. 我们可以设\(dp[i][j]\)表示到了第\(i\ ...
- [CSP-S模拟测试]:e(树上主席树)
题目传送门(内部题66) 输入格式 第一行,一个正整数$n$,一个自然数$q$,一个整数$type$.第二行,$n$个正整数,代表$a_i$.接下来$n-1$行,每行两个正整数$u$.$v$,代表树中 ...
- 转载:String.format()的详细用法
转载自:https://blog.csdn.net/anita9999/article/details/82346552 问题 在开发的时候一段字符串的中间某一部分是需要可变的 比如一个Textvie ...
- 嵌入式Linux文件系统知多少
Nand/Nor Flash 在嵌入式Linux产品中,通常使用的存储介质为Nand Flash和Nor Flash,而手机.相机等产品通常使用eMMC.SD Card作为存储介质,导致这种差异的原因 ...
- ora600
4节点RAC:版本oracle11.2.0.4 22:20——23:40发生ora600 alert日志: Errors in file /u01/app/oracle/diag/rdbms/orcl ...