zoukankan      html  css  js  c++  java
  • 使用Apache CXF和Spring集成创建Web Service(zz)

    使用Apache CXF和Spring集成创建Web Service

    您的评价:
         
    还行
     收藏该经验    
     

    1.创建HelloWorld 接口类

     

    1 package com.googlecode.garbagecan.cxfstudy.helloworld;
    2 import javax.jws.WebParam;
    3 import javax.jws.WebResult;
    4 import javax.jws.WebService;
    5 @WebService
    6 public interface HelloWorld {
    7     public @WebResult(name="String")String sayHi(@WebParam(name="text") String text);
    8 }

     

     

    2.创建HelloWorld实现类

     

    1 package com.googlecode.garbagecan.cxfstudy.helloworld;
    2 public class HelloWorldImpl implements HelloWorld {
    3     public String sayHi(String name) {
    4         String msg = "Hello " + name + "!";
    5         System.out.println("Server: " + msg);
    6         return msg;
    7     }
    8 }

     

     

    3.修改web.xml文件

     

    01 <!DOCTYPE web-app PUBLIC
    02  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    03  "http://java.sun.com/dtd/web-app_2_3.dtd" >
    04 <web-app>
    05     <display-name>cxfstudy</display-name>
    06     <servlet>
    07         <servlet-name>cxf</servlet-name>
    08         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    09         <load-on-startup>1</load-on-startup>
    10     </servlet>
    11     <servlet-mapping>
    12         <servlet-name>cxf</servlet-name>
    13         <url-pattern>/ws/*</url-pattern>
    14     </servlet-mapping>
    15     <listener>
    16         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    17     </listener>
    18      
    19     <context-param>
    20         <param-name>contextConfigLocation</param-name>
    21         <param-value>classpath*:**/spring.xml</param-value>
    22     </context-param>
    23      
    24 </web-app>

     

     

    4.创建spring配置文件并放在classpath路径下

     

    01 <?xml version="1.0" encoding="UTF-8"?>
    02 <beans xmlns="http://www.springframework.org/schema/beans"
    03     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jaxws="http://cxf.apache.org/jaxws"
    04     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
    05 http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
    06     <import resource="classpath:META-INF/cxf/cxf.xml" />
    07     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    08     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    09     <jaxws:endpoint id="helloworld"implementor="com.googlecode.garbagecan.cxfstudy.helloworld.HelloWorldImpl"address="/HelloWorld" />
    10      
    11     <!-- For client test -->
    12     <jaxws:client id="helloworldClient" address="http://localhost:9000/ws/HelloWorld"serviceClass="com.googlecode.garbagecan.cxfstudy.helloworld.HelloWorld" />  
    13 </beans>

     

     

    5.创建测试类

     

    01 package com.googlecode.garbagecan.cxfstudy.helloworld;
    02 import org.springframework.context.ApplicationContext;
    03 import org.springframework.context.support.ClassPathXmlApplicationContext;
    04 public class SpringClient {
    05     public static void main(String[] args) {
    06         ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    07         HelloWorld helloworld = (HelloWorld)context.getBean("helloworldClient");
    08         System.out.println(helloworld.sayHi("kongxx"));
    09     }
    10 }

     

     

    6.测试

    6. 1.首先启动tomcat或者使用maven的jetty,并访问http://localhost:9000/ws/HelloWorld?wsdl来验证web service已经启动并且生效;
    6. 2.然后运行测试类来验证web service。

  • 相关阅读:
    分享最好的HTML5编码教程和参考手册
    随机字符变换效果的jQuery插件开发教程
    拒绝用SEO的眼光来设计你的Meta标签
    GBin1教程:使用jQuery插件jquery.validationEngine实现表单验证功能
    vs 2010 程序发布时出现 TransformXml任务意外失败
    CSS hack
    javascript div 弹出可拖动窗口
    Javascript String类的属性及方法
    兼容浏览器的布局CSS
    该伙伴事务管理器已经禁止了它对远程/网络事务的支持
  • 原文地址:https://www.cnblogs.com/frankly-frank/p/3483556.html
Copyright © 2011-2022 走看看