java的WebService实践(cxf)
Java发布WebService,结合Spring,通过cxf的方式
难点:1、引用什么jar包;
1、创建接口
源码如下:
package com.nankang; import javax.jws.WebParam;
import javax.jws.WebService; @WebService
public interface HelloWorld {
String sayHi(@WebParam(name="text") String text);
}
2、实现接口
源码如下:
package com.nankang; import javax.jws.WebService; @WebService(endpointInterface="com.nankang.HelloWorld",serviceName="HelloWorld")
public class HelloWorldImpl implements HelloWorld { public String sayHi(String text) {
// TODO Auto-generated method stub
return "Hello" + text;
} }
3、web.xml的配置
源码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.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>/webservice/*</url-pattern>
</servlet-mapping>
</web-app>
4、添加applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.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-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <!-- 扫描spring注解配置 -->
<context:component-scan base-package="com.nankang.contactqueryservice" /> <jaxws:endpoint id="helloWorld" implementor="com.nankang.HelloWorldImpl"
address="/helloWorld" /> </beans>
5、访问
http://localhost:8080/WebServiceTest/webservice/helloWorld?wsdl
6、访问源码
package com.nankang; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloWorldClient { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
svr.setServiceClass(HelloWorld.class);
svr.setAddress("http://localhost:8080/WebServiceTest/webservice/helloWorld");
HelloWorld hw = (HelloWorld)svr.create();
System.out.println(hw.sayHi("ddddaaaa"));
}
}
7、发布示例:
package com.nankang;
import javax.xml.ws.Endpoint;
public class WebServiceApp {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("web service start");
HelloWorldImpl implementor = new HelloWorldImpl();
String address = "http://localhost:8080/helloWorld";
Endpoint.publish(address, implementor);
System.out.println("web service started");
}
}
参考:
http://blog.sina.com.cn/s/blog_a0e7e34c0101959p.html
http://www.cnblogs.com/frankliiu-java/articles/1641949.html
java的WebService实践(cxf)的更多相关文章
- 转-JAVA webservice之CXF 范例--http://cxshun.iteye.com/blog/1275408
JAVA webservice之CXF 博客分类: j2ee相关 昨天我们一起学习了一下xfire,今天我们来看一下CXF,为什么学完那个接着学这个呢.因为CXF是在xfire的基础上实现 的,所以我 ...
- Java调用WebService方法总结(7)--CXF调用WebService
CXF = Celtix + XFire,继承了Celtix和XFire两大开源项目的精华,是一个开源的,全功能的,容易使用的WebService框架.文中所使用到的软件版本:Java 1.8.0_1 ...
- java 调用webservice的各种方法总结
java 调用webservice的各种方法总结 几种流行的开源WebService框架Axis1,Axis2,Xfire,CXF,JWS比较 方法一:创建基于JAX-WS的webservice(包括 ...
- Java之webService知识
Java之webService知识 1 webservice基础知识 1.1 webService请求的本质 一次webService本质请求,如下所示: 1.2 wsdl文档解析 wsdl文档元素结 ...
- WebService之CXF注解报错(一)
WebService之CXF注解 1.详细报错例如以下 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] ...
- WebService它CXF注释错误(两)
WebService它CXF注解 1.详细报错例如以下 五月 04, 2014 11:24:12 下午 org.apache.cxf.wsdl.service.factory.ReflectionSe ...
- WebService之CXF注解报错(三)
WebService之CXF注解 1.具体错误如下 五月 04, 2014 11:29:28 下午 org.apache.cxf.wsdl.service.factory.ReflectionServ ...
- WebService之CXF注解报错(二)
WebService之CXF注解 1.具体报错如下 五月 04, 2014 11:24:12 下午 org.apache.cxf.wsdl.service.factory.ReflectionServ ...
- C++与Java通过WebService通信(上)
一. 前言 本篇讲述如果通过C++客户端访问Java服务端发布的SOAP模式的WebService接口.文档中的样例代码拷贝出去即可运行,所有的代码都是本地测试OK的:本文不但解决了接口调用的问题,同 ...
随机推荐
- Apache2 CGI demo
1. 修改 httpd.conf 配置 <IfModule alias_module> ScriptAlias /cgi-bin/ "/usr/local/apache2/cg ...
- unsigned char 转字符串:
通常送显示的都是字符串,对于int long float转字符串有对应的函数,还有sprintf进行格式输出,对于嵌入式和单片机大多都用unsigned char型变量,转字符串需要自己编写函数,需要 ...
- firebug下载时出现there was an error loading firebug
打开Firefox -> Preferences -> Advance ->Certificates 将Query OSCP....前面的checkbox取消
- iOS8 StoryBoard 连线diss方法
添加自定义Dismiss类: // Dismiss.h // StoryBoardTest // // Created by zhujin on 14/12/23. // Copyright ...
- 对 HTTP 304 的理解(转-并增加自己的测试)
作者:吴俊杰 性别:男 邮箱:sshroot@126.com 文章类型:原创 博客:http://www.cnblogs.com/voiphudong/ 转自: http://www.cnblogs. ...
- WebService中实现上传下载文件
不多说,直接看代码: /*上传文件的WebService*/ using System; using System.Collections; using System.Collections.Gene ...
- cvs 用法
CVS使用指南 1 概念 CVS是Client/Server结构的并行版本控制系统. 资源库(repository) 存在于服务器上,所有版本的数据仓库.可以把它想象成一个数据库服务器. 模块 (mo ...
- Export excel file using web API
使用MVC controller输出excel的例子,自不待言,例子满天飞. 由于本项目使用的是Asp.net MVC API,因此在本项目使用API,实现了文件下载功能.代码的原理很简单,基本上是老 ...
- XAML学习笔记
XAML是用于实例化.NET对象的标记语言,主要用于构造WPF界面.不同于WPF之前的Windows编程技术(WinForm,MFC及win32sdk),在WPF之中界面主要是在XAML中添 ...
- Android数据的四种存储方式
作为一个完成的应用程序,数据存储操作是必不可少的.因此,Android系统一共提供了四种数据存储方式.分别是:SharePreference.SQLite.Content Provider和File. ...