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. Spring Bean 生命周期2

    在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一个实例,而不是每次都产生一个新的对象使用Sin ...

  2. Tomcat发布项目时,浏览器地址栏图标的问题

    最近在做一个Java网络应用程序,服务器是tomcat.在默认情况下,当用户访问该网络应用时,地址栏图标显示为tomcat猫.我希望把它换成自己的图标,于是研究了一下.在研究过程中,我发现网上的资料大 ...

  3. http 303 307 302 状态码理解

    最近在看 <<the rails4 way>> 书中提到了这几个状态码,网上搜到几篇文章 http://www.cnblogs.com/cswuyg/p/3871976.htm ...

  4. springmvc数据处理模型

    1.ModelAndView 实现: @RequestMapping("/testModelAndView") public ModelAndView testModelAndVi ...

  5. 软件开发过程文档-cgaowei

    鸡肋——食之无味,弃之可惜”,软件开发过程文档遭遇了鸡肋一样的境遇. 目前敏捷软件开发过程非常流行.相对于软件开发过程文档,敏捷软件开发过程更加重视可运行的程序.关于软件开发过程文档,两个极端都是不可 ...

  6. PHP 连接 MSSQL

    1 设置 2 php 代码: <?php header('Content-Type:text/html; charset=GBK'); define('DB_HOST','localhost') ...

  7. http.StripPrefix 的参数含义

    看下面两行代码: http.Handle("/file/", http.StripPrefix("/file", http.FileServer(http.Di ...

  8. How do I implement a cancelable event?

    n System.ComponentModel, there's a class called CancelEventArgs which contains a Cancel member that ...

  9. 最大子序列和(O(n))

    下面介绍一个线性的算法,这个算法是许多聪明算法的典型:运行时间是明显的,但是正确性则很不明显(不容易理解). //线性的算法O(N) long maxSubSum4(const vector<i ...

  10. OC基础(15)

    @property参数 @Property练习 @class 循环retian *:first-child { margin-top: 0 !important; } body > *:last ...