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" ...
随机推荐
- Jpa 的Persistence.xml配置讲解
<?xml version="1.0"?> <persistence xmlns="http://java.sun.com/xml/ns/persist ...
- 2 Java基础语法(keyword,标识符,凝视,常量,进制转换,变量,数据类型,数据类型转换)
1:keyword(掌握) (1)被Java语言赋予特定含义的单词 (2)特点: 所有小写. (3)注意事项: A:goto和const作为保留字存在. B:类似于Notepad++这种高级记事本会对 ...
- [Angular] Use Angular style sanitization to mark dynamic styles as trusted values
Angular has a very robust security model. Dynamically inserted html, style or url values into the DO ...
- echarts+百度地图+vue 填坑记(一)(百度地图、鼠标移入移出标注,信息框会产生闪烁)
大概七月底开始实习,到现在经历了两个完整的项目(c2b). 因为开发时间紧,任务重,所以在开发过程踩到的坑都没时间去记录. 现在在开发一个某链运输监控系统,到了收尾阶段,有时间写博客了!开心! 一.鼠 ...
- PatentTips - Handling shared interrupts in bios under a virtualization technology environment
BACKGROUND This relates to the operation of software under a virtualization technology (VT) environm ...
- css3-13 如何改变文本框的轮廓颜色
css3-13 如何改变文本框的轮廓颜色 一.总结 一句话总结:outline使用和border很像,几乎一模一样,多了一个offset属性 1.轮廓outline如何使用? 使用和border很像, ...
- UI组件之TextView及其子类(三)ToggleButton和Switch
ToggleButton.Switch.CheckBox和RadioButton都是继承自android.widget.CompoundButton,意思是可选择的,因此它们的使用方法都非常类似. C ...
- iOS开发RunnLoop学习二:GCD中的定时器
#import "ViewController.h" @interface ViewController () /** 注释 */ @property (nonatomic, st ...
- python opencv3 —— findContours
findContours 是 opencv 下的轮廓提取函数. 1. api 分析 findContours(image, mode, method[, contours[, hierarchy[, ...
- Visual Stdio 环境下使用 GSL (GNU Scientific Library)
Visual Stdio 环境下使用 GSL (GNU Scientific Library) 经測试.这里的方法不适用于VS2015. * 这篇文章有点过时了.建议从以下网址下载能够在 vs 环境下 ...