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)的更多相关文章

  1. 转-JAVA webservice之CXF 范例--http://cxshun.iteye.com/blog/1275408

    JAVA webservice之CXF 博客分类: j2ee相关 昨天我们一起学习了一下xfire,今天我们来看一下CXF,为什么学完那个接着学这个呢.因为CXF是在xfire的基础上实现 的,所以我 ...

  2. Java调用WebService方法总结(7)--CXF调用WebService

    CXF = Celtix + XFire,继承了Celtix和XFire两大开源项目的精华,是一个开源的,全功能的,容易使用的WebService框架.文中所使用到的软件版本:Java 1.8.0_1 ...

  3. java 调用webservice的各种方法总结

    java 调用webservice的各种方法总结 几种流行的开源WebService框架Axis1,Axis2,Xfire,CXF,JWS比较 方法一:创建基于JAX-WS的webservice(包括 ...

  4. Java之webService知识

    Java之webService知识 1 webservice基础知识 1.1 webService请求的本质 一次webService本质请求,如下所示: 1.2 wsdl文档解析 wsdl文档元素结 ...

  5. WebService之CXF注解报错(一)

    WebService之CXF注解 1.详细报错例如以下 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] ...

  6. WebService它CXF注释错误(两)

    WebService它CXF注解 1.详细报错例如以下 五月 04, 2014 11:24:12 下午 org.apache.cxf.wsdl.service.factory.ReflectionSe ...

  7. WebService之CXF注解报错(三)

    WebService之CXF注解 1.具体错误如下 五月 04, 2014 11:29:28 下午 org.apache.cxf.wsdl.service.factory.ReflectionServ ...

  8. WebService之CXF注解报错(二)

    WebService之CXF注解 1.具体报错如下 五月 04, 2014 11:24:12 下午 org.apache.cxf.wsdl.service.factory.ReflectionServ ...

  9. C++与Java通过WebService通信(上)

    一. 前言 本篇讲述如果通过C++客户端访问Java服务端发布的SOAP模式的WebService接口.文档中的样例代码拷贝出去即可运行,所有的代码都是本地测试OK的:本文不但解决了接口调用的问题,同 ...

随机推荐

  1. js对象继承

    方法: 1.原型链继承 2.使用对象冒充继承

  2. TableViewCell的分割线显示不全解决方法

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath ...

  3. jenkins参数化构建过程

    http://www.cnblogs.com/meitian/p/5492457.html 1,首先增加Jenkisn插件 https://wiki.jenkins-ci.org/display/JE ...

  4. plsql如果表和函数等显示不出来

    就把用户设为所有用户,所有的东西就会都显示出来了,然后再把用户切换为当前用户和My objects,你想看的东西就全部显示出来了.

  5. [Java] java中的异常处理-续

    异常处理器,它由try, catch, finally以及随后的程序块组成.finally不是必须的. catch的括号有一个参数,代表所要捕捉的异常的类型.catch会捕捉相应的类型及其衍生类.tr ...

  6. clone代码

    例子1:

  7. MySql 申明变量以及赋值

    sql server中变量要先申明后赋值: 局部变量用一个@标识,全局变量用两个@(常用的全局变量一般都是已经定义好的): 申明局部变量语法:declare @变量名 数据类型:例如:declare ...

  8. CodeForces 604D 【离散数学 置换群】

    题意: 给你一个方程,方程太变态不打,给你一个p一个k,p保证是大于等于3的质数,k保证在0~p-1之间的整数.要求对应函数的定义域在0~p-1值域为0~p-1的子集,求这样的函数有多少个... 分析 ...

  9. python正则表达式介绍

    点击打开链接 1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分.正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的 ...

  10. oracle行列转换函数的使用

    oracle 10g wmsys.wm_concat行列转换函数的使用: 首先让我们来看看这个神奇的函数wm_concat(列名),该函数可以把列值以","号分隔起来,并显示成一行 ...