Spring 组cxf宣布webservice
通过spring宣布webservice接口
spring jar包+cxf jar Java包 下列文件丢失jar包需要下载自己
项目结构
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaXRscWk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
1、实体类
package com.test.entity;
public class User {
public User(String name, String pwd, String sex) {
super();
this.name = name;
this.pwd = pwd;
this.sex = sex;
}
private String name;
private String pwd;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
2、webservice接口
package com.test.webservice; import java.util.List; import javax.jws.WebService; import com.test.entity.User; /**
* 声明 webService 接口
* @author Administrator
*
*/
@WebService
public interface IWebservice { public String say(String str);
public String findList();
}
3、接口实现
package com.test.webservice.imp; import java.util.ArrayList;
import java.util.List; import javax.jws.WebService; import net.sf.json.JSONArray; import com.test.entity.User;
import com.test.webservice.IWebservice; /**
* 声明这个类是 :IWebservice实现类
* @author Administrator
*
*/ @WebService(endpointInterface="com.test.webservice.IWebservice")
public class WebServiceImpl implements IWebservice { public String say(String str) { return str;
} public String findList(){ List<User> u=new ArrayList<User>(); u.add(new User("张三", "12343", "男"));
u.add(new User("妮妮", "12343", "女"));
u.add(new User("利弊", "9900000", "女")); JSONArray json=JSONArray.fromObject(u); return json.toString();
}
}
4、cxf-service.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">
<!--
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
-->
<!-- id 自己定义 serviceClass接口完整包名 address訪问地址 -->
<jaxws:server id="webservice" serviceClass="com.test.webservice.IWebservice" address="/webservice">
<jaxws:serviceBean>
<!-- 实现类 -->
<bean class="com.test.webservice.imp.WebServiceImpl" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
5、web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>cxf_service</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file> </welcome-file-list> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/config/cxf-service.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping> </web-app>
6、client调用接口
package com.test.web; import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsClientFactoryBean;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import com.test.webservice.IWebservice; public class TestWebService { public static void main(String[] args) { //调用WebService // JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//
// factory.setServiceClass(IWebservice.class);
//
// factory.setAddress("http://localhost:8080/cxf_service/services/webservice");
//
// IWebservice service = (IWebservice) factory.create();
// System.out.println("#############Client getUserByName##############");
// System.out.println(service.say("ABC")); //创建JaxWsDynamicClientFactory 实例
JaxWsDynamicClientFactory fdc=JaxWsDynamicClientFactory.newInstance();
//获取链接
Client client= fdc.createClient("http://localhost:8080/cxf_service/services/webservice? wsdl"); //调用接口名称 上传參数 返回结果
Object[] obj=null;
Object[] objs=null;
try {
//运行指定方法 say 方法名 參数 obj = client.invoke("say","我叫张三");
//运行制定方法 findList 方法名
objs = client.invoke("findList"); System.out.println(obj[0].toString());
System.out.println(objs[0].toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Spring 组cxf宣布webservice的更多相关文章
- struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
Cxf + Spring+ myeclipse+ cxf 进行 Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...
- Spring Boot+CXF搭建WebService(转)
概述 最近项目用到在Spring boot下搭建WebService服务,对Java语言下的WebService了解甚少,而今抽个时间查阅资料整理下Spring Boot结合CXF打架WebServi ...
- Spring集成CXF发布WebService并在客户端调用
Spring集成CXF发布WebService 1.导入jar包 因为官方下载的包里面有其他版本的sprring包,全导入会产生版本冲突,所以去掉spring的部分,然后在项目根目录下新建了一个CXF ...
- Spring boot+CXF开发WebService
最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...
- Spring boot+CXF开发WebService Demo
最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...
- Spring MVC + CXF实现webservice接口
本来都是WebAPI/RestfulAPI接口对外提供接口的,突然有个需求要提供WebService接口,本想着Spring和CXF这么成熟的两个产品,这么的也整不出什么幺蛾子来啊. 结果还真是出了几 ...
- spring 与 CXF 整合 webservice 出现error “Unable to locate Spring NamespaceHandler for XML schema namespace” 总结
我试了多个版本的spring 发现 出现error : Unable to locate Spring NamespaceHandler for XML schema namespace 并非都是sp ...
- spring集成cxf实现webservice接口功能
由于cxf的web项目已经集成了Spring,所以cxf的服务类都是在spring的配置文件中完成的.以下是步骤:第一步:建立一个web项目.第二步:准备所有jar包.将cxf_home\lib项目下 ...
- Spring Boot+CXF搭建WebService
Spring Boot WebService开发 需要依赖Maven的Pom清单 <?xml version="1.0" encoding="UTF-8" ...
随机推荐
- [React] Remove React PropTypes by using Flow Annotations (in CRA)
Starting from v15.5 if we wanted to use React's PropTypes we had to change our code to use a separat ...
- ASP.NET MVC 入门4、Controller与Action
原帖地址:http://www.cnblogs.com/QLeelulu/archive/2008/10/04/1303672.html Controller是MVC中比較重要的一部分.差点儿全部的业 ...
- iOS_06_基本运算符
一.算术运算 c语言一共有34种运算符,包括了常见的加减乘除 1.加法运算+ # 除了能做加法运算,还能表示正号:+5.+90 2.减法运算- # 除了能做减法运算,还能表示符号:-10.-200 3 ...
- gvim不能直接打开360压缩打开的文件
1. 压缩文件a.rar 2. 默认使用360压缩打开 3.用gvim打开对应的a.c文件,提示permission denied 4.用gvim跟踪目录,发现360管理的缓冲目录无法打开 原因未分析 ...
- [TypeScript] Union Types and Type Aliases in TypeScript
Sometimes we want our function arguments to be able to accept more than 1 type; e.g. a string or an ...
- POJ 2546 Circular Area 几何
http://poj.org/problem?id=2546 晚上发现鼠标快不行了了!!!鼠标你肿么了,肿么突然就按键不灵了,哭,谁送我一只呀,奖励我舍友一只.哈哈.开玩笑滴~ 舍友大怒说" ...
- FeatureLayer,FeatureDataset,FeatureClass,Feature的区别与联系总结
duckweeds原文 FeatureLayer,FeatureDataset,FeatureClass,Feature几个概念一点点总结,欢迎指教 刚学AE,其中很多概念都模糊不清.经过一段时间的摸 ...
- windows2003 IIS6下安装ISAPI_Rewrite3破解版
摘抄的https://jingyan.baidu.com/article/ff42efa931a2c0c19e220298.html 非常感谢,我是怕百度经验有一天消失了,以防万一 iis6 ISAP ...
- 【50.00%】【codeforces 602C】The Two Routes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- [Docker] Run, Stop and Remove Docker Containers
In this lesson, we'll find out the basics of running Docker containers. We'll go over how to downloa ...