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 前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要 ...
随机推荐
- go语言学习——变量、常量、循环、条件、switch、数组和切片
1.变量 package main import "fmt" func main() { 个或者多个变量. var a string = "initial" f ...
- Intelli IDEA快捷键(配合IdeaVim)(转)
Intelli IDEA快捷键(配合IdeaVim)(转) Intelli IDEA开发环境,个人总结的一些常用的快捷键. 想要使用vim方式编辑代码,可以使用Intelli IDEA的IdeaVim ...
- Thymeleaf 整理
1.标准变量表达式: thymeleaf中的变量表达式使用${变量名}的方式获取其中的数据 th:text="" 是thymeleaf的一个属性,用于文本的显示 如:<spa ...
- Django【第11篇】:Django之分页升级版本(组件)
分页组件 一.分页的实现与使用 class Pagination(object): """ 自定义分页 """ def __init__(s ...
- python中strftime和strptime函数
strftime和strptime函数均来自包datetime from datetime import * strftime: 将datetime包中的datetime类,按照入参格式生成字符串变量 ...
- delphi 10.3.1 android沉浸式透明状态栏
从10.2升级上来, 之前的沉浸状态栏在android手机上不透明了, 添加二个发布文件,remote path分别设为 res\values-v21和 res\values-v19 style.xm ...
- HTML和CSS遇到的细节问题
一.列表项标记窜出div盒子 列表项标记窜出盒子,是因为设置了 *; } ,消除了<li>元素的默认外边距. 结解决方法:消除*{}选择器或是设置外边距 列表项目标记与边距有关 二.div ...
- CSS盒子模型与怪异盒模型
盒子模型(Box Modle)可以用来对元素进行布局,包括内边距,边框,外边距,和实际内容这几个部分. 盒子模型分为两种 第一种是W3c标准的盒子模型(标准盒模型) .第二种IE标准 ...
- React Native 之FlatList 下拉刷新和上拉加载更多
接上一篇代码: 只修改了FlatListDemo.js里面的代码 import React, {Fragment,Component} from 'react'; import { SafeAreaV ...
- luogu 4004 Hello world! 分块 + 并查集 + 乱搞
其实呢,我也不理解这道题咋做,等以后有时间再研究研究 #include <bits/stdc++.h> #define ll long long #define maxn 100002 u ...