- 浏览: 16093 次
- 性别:
- 来自: 北京
文章分类
最新评论
我们的环境是eclipse , maven。
在开发java webservice时,有两个比较流行的框架:axis2和cxf。cxf可以无缝的和spring集成,而axis2需要打包成aar文件,在tomcat下面跑。于是我们选择了cxf开发webservice。
下面一步步的介绍用cxf开发服务端和客户端的例子。
首先开发服务端:
一:在pom.xml文件中加入依赖:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-api</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-bindings-soap</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-security</artifactId>
<version>2.5.0</version>
</dependency>
然后会在maven dependencies文件夹中看到引用的jar包
二:开发服务的接口类和实现类
首先接口类:
package com.mycompany.webservice.server;
import javax.jws.WebService;
@WebService
public interface Greeting {
public String greeting(String userName);
}
然后实现类:
package com.mycompany.webservice.server;
import java.util.Calendar;
import javax.jws.WebService;
@WebService(endpointInterface = "com.mycompany.webservice.server.Greeting")
public class GreetingServiceImpl implements Greeting {
public String greeting(String userName) {
return "Hello " + userName + ", currentTime is "
+ Calendar.getInstance().getTime();
}
}
注意上面实现类的endpointInterface的接口类路径不要写错,否则会报Could not load Webservice SEI
服务端的代码开发就到此了。下面要配置文件。
在application.xml中:
添加
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/cache/spring-context-3.2.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"
default-autowire="byName" default-lazy-init="true">
<!--实现类的bean,需要spring注入 -->
<bean id="greetingServiceImpl" class="com.mycompany.webservice.server.GreetingServiceImpl"/>
<jaxws:endpoint id="greetingService" implementor="#greetingServiceImpl" address="/Greeting" />
在web.xml中:
添加
<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>
<!--==这个设置很重要,那么我们的webservice的地址就是http://localhost:8080/yourProgramName/webservice/Greeting===-->
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
好了。到此我们的服务端就开发好了。。
就可以通过http://localhost:8080/yourProgramName/webservice/Greeting?wsdl 访问看到我们的wsdl了。
下面开发我们的客户端,来调用上面的服务。
package com.mycompany.webservice.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class GreetingServiceClient {
public static void main(String[] args) {
//创建WebService客户端代理工厂
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//注册WebService接口
factory.setServiceClass(Greeting.class);
//设置WebService地址
factory.setAddress("http://localhost:8080/yourProgramName/webservice/Greeting");
Greeting greetingService = (Greeting)factory.create();
System.out.println("invoke webservice...");
System.out.println("message context is:"+greetingService.greeting("gary"));
}
}
好了客户端也开发完成。
在开发java webservice时,有两个比较流行的框架:axis2和cxf。cxf可以无缝的和spring集成,而axis2需要打包成aar文件,在tomcat下面跑。于是我们选择了cxf开发webservice。
下面一步步的介绍用cxf开发服务端和客户端的例子。
首先开发服务端:
一:在pom.xml文件中加入依赖:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-api</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-bindings-soap</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-security</artifactId>
<version>2.5.0</version>
</dependency>
然后会在maven dependencies文件夹中看到引用的jar包
二:开发服务的接口类和实现类
首先接口类:
package com.mycompany.webservice.server;
import javax.jws.WebService;
@WebService
public interface Greeting {
public String greeting(String userName);
}
然后实现类:
package com.mycompany.webservice.server;
import java.util.Calendar;
import javax.jws.WebService;
@WebService(endpointInterface = "com.mycompany.webservice.server.Greeting")
public class GreetingServiceImpl implements Greeting {
public String greeting(String userName) {
return "Hello " + userName + ", currentTime is "
+ Calendar.getInstance().getTime();
}
}
注意上面实现类的endpointInterface的接口类路径不要写错,否则会报Could not load Webservice SEI
服务端的代码开发就到此了。下面要配置文件。
在application.xml中:
添加
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/cache/spring-context-3.2.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"
default-autowire="byName" default-lazy-init="true">
<!--实现类的bean,需要spring注入 -->
<bean id="greetingServiceImpl" class="com.mycompany.webservice.server.GreetingServiceImpl"/>
<jaxws:endpoint id="greetingService" implementor="#greetingServiceImpl" address="/Greeting" />
在web.xml中:
添加
<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>
<!--==这个设置很重要,那么我们的webservice的地址就是http://localhost:8080/yourProgramName/webservice/Greeting===-->
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
好了。到此我们的服务端就开发好了。。
就可以通过http://localhost:8080/yourProgramName/webservice/Greeting?wsdl 访问看到我们的wsdl了。
下面开发我们的客户端,来调用上面的服务。
package com.mycompany.webservice.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class GreetingServiceClient {
public static void main(String[] args) {
//创建WebService客户端代理工厂
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//注册WebService接口
factory.setServiceClass(Greeting.class);
//设置WebService地址
factory.setAddress("http://localhost:8080/yourProgramName/webservice/Greeting");
Greeting greetingService = (Greeting)factory.create();
System.out.println("invoke webservice...");
System.out.println("message context is:"+greetingService.greeting("gary"));
}
}
好了客户端也开发完成。
发表评论
-
SpringMVC+ajaxFileUpload上传图片 IE浏览器弹下载框问题解决方案
2017-05-08 08:42 286SpringMVC+ajaxFileUpload上传图片 IE ... -
网站架构之架构演化
2017-03-30 13:50 296http://www.cnblogs.com/pflee/p/ ... -
线程,
2017-03-10 09:07 271http://www.cnblogs.com/wxd0108/ ... -
springmvc<一>一种资源返回多种形式【ContentNegotiatingViewResolver】
2017-03-01 09:53 403springmvc<一>一种资源返回多种形式【Co ... -
<context-param>与<init-param>的区别与作用(转载)
2017-02-28 13:45 360<context-param>的作用: web.x ... -
sqlserver日期格式化函数
2016-09-21 10:16 428Sql Server 中一个非常强大的日期格式化函数 ... -
http://blog.csdn.net/zuoluoboy/article/details/19766131(转载)
2016-07-12 10:04 854Spring工作流程描述 ... -
玩转cookie(转载度娘http://jingyan.baidu.com/article/7c6fb4281f4e5580642c90d0.html)
2015-11-09 10:53 706怎么在java中增加一个cookie; 怎么在java中修改 ... -
jquery插件开发(http://www.poluoluo.com/jzxy/201204/163035.html)
2015-01-25 21:40 496一.介绍 插件编写的目 ... -
hibernate各种查询汇总(转载)
2014-12-23 15:16 3331. 查询整个映射对象所有字段 Java代码 // ... -
hibernate各种查询汇总(转载)
2014-12-23 15:16 01. 查询整个映射对象所有字段 Java代码 // ... -
JAVA多线程实现和应用总结(转载自http://www.cnblogs.com/yezhenhan/archive/2012/01/09/2317636.)
2014-12-22 15:27 474JAVA多线程实现和应用总 ... -
ResultSet rs 之 rs.next()(转载自百度一哥们)
2014-12-16 15:10 756ResultSet rs是结果集。查询出的记录是一个列表,初始 ... -
ResultSet rs 之 rs.next()
2014-12-16 15:10 803ResultSet rs是结果集。查询出的记录是一个列表,初始 ... -
java substring
2014-12-15 17:12 495java substring 方法 str=str.subst ... -
PLSQL插入数据中文乱码的问题(转)
2014-11-26 16:33 3893PLSQL插入数据中文乱码的问题 www.111cn.net ... -
遍历Map的四种方法 (转载)
2014-11-25 10:21 370public static void main(String[ ... -
sql执行顺序
2014-10-08 09:57 271SQL Select语句完整的执行顺序:1、from子句组装来 ... -
jms简明学习教程(转载)
2014-09-04 15:55 416基础篇 JMS是应用系统或组件之间相互通信的应用程序接口,利 ... -
MYSQL数据库事务处理和锁机制(转载)
2014-09-03 14:37 764一、数据库事务的百科 数据库事务(Database Tran ...
相关推荐
NULL 博文链接:https://zw7534313.iteye.com/blog/2250409
简单的webservice+Cxf+Spring数据对接实例以及jar.rar简单的webservice+Cxf+Spring数据对接实例以及jar.rar简单的webservice+Cxf+Spring数据对接实例以及jar.rar简单的webservice+Cxf+Spring数据对接实例以及jar.rar...
webservice cxf 客户端创建实例
通过eclipse发布简单的webservice.
webservice天气预报例子cxf实例,
webservice之cxf实例............
CXF WebService java实例 例子包括基本的 HelloWord列子 还有传递List ,Map的列子 另外还有webservice 文件下载上传的示例
WebService CXF同步通信接口编程和Mybatis , Spring 的简单应用。值得初学者和开发中用到的同学借鉴。
webservice cxf spring整合返回list,bean,string,json,xml项目完整实例
SSH整合cxf webservice实例
CXF实现WebService详解实例
springmvc+webservice(cxf)+maven 完整实例,下载直接可以运行,非常实用
学习CXF WebService入门实例一.pdf学习CXF WebService入门实例一.pdf
Spring + cxf = webservice 完整实例源码免费下载 完全免费。此资源仅为文档提供。 版权为百度文档 "Spring + cxf = webservice 完整实例源码免费下载" 所有。
gsoap CXF2.7.5 ssh vc++ webservice应用实例的源码。具体内容看博文《gsoap CXF2.7.5 ssh vc++ webservice应用实例(一)(二)(三)(四)》 http://blog.csdn.net/biboheart/article/details/11959935
使用CXF开发简单HelloWorld实例
cxf webservice 与spring整合的开发实例
关于WebService的实例及相关axis/apache-cxf的应用关于WebService的实例及相关axis/apache-cxf的应用
CXF-webService实例(eclipse工程,有jar包,可运行) jax-ws和pojo两种方式发布
CXF实例源代码(服务器端) 入门教程 webservice 实例