zoukankan      html  css  js  c++  java
  • 使用cxf写web service的简单实例

    增加CXF依赖

    <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>apache-cxf</artifactId>
            <version>${cxf.version}</version>
            <type>pom</type>
      </dependency>

    创建服务接口

    import javax.jws.WebService;
    
    @WebService
    public interface HelloWorld {
        public String sayHi(String text);
    }
    
    import javax.jws.WebService;
    
    @WebService
    public class HellowWordController implements HelloWorld {
        /*
         * (non-Javadoc)
         * 
         * @see com.wfj.infc.HelloWorld#sayHi(java.lang.String)
         */
        public String sayHi(String text) {
            // TODO Auto-generated method stub
            System.out.println("sayHi called");
            return "Hello " + text;
        }
    
    }

    cxfdemo-beans.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:jaxws="http://cxf.apache.org/jaxws"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
              http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
           default-autowire="byName">
    
        <import resource="classpath:META-INF/cxf/cxf.xml"/>
        <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
        <bean id="hello" class="com.wangfj.product.core.controller.HellowWordController"/>
        <jaxws:endpoint id="helloWorld" implementor="#hello" address="/webservice/hw"/>
    </beans>

    在webapp中发布

    XF提供了spring的集成,同时还提供了org.apache.cxf.transport.servlet.CXFServlet用于在 web容器中发布WebService。 前面的例子中增加了整个apache-cxf的依赖,所以会自动增加对srping的引用。只需要写beans配置文件和web.xml文件即可。

    • 在web.xml中配置CXFServlet
     <servlet>
            <servlet-name>CXFServlet</servlet-name>
            <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>CXFServlet</servlet-name>
            <url-pattern>/services/*</url-pattern>
        </servlet-mapping>
     <context-param>
             <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/cxfdemo-beans.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
  • 相关阅读:
    python 基础(三) list 的用法
    python 基础(二) string 的用法
    python 基础(四)购物车(list类型练习)
    python 基础(一)猜年龄游戏
    centOS 7 环境搭建之安装 python 3
    centOS 7 环境搭建之安装 JDK 8
    源代码管理相关命令(Git常用命令、Nuget常用命令、CMD常用命令)
    .Net Core 3.0 关于Windows Form和WPF的全面支持
    .Net Core 常用开发工具(IDE和运行时、Visual Studio插件、Visual Studio Code插件)
    .Net Core 精选公众号集合(保持更新)
  • 原文地址:https://www.cnblogs.com/KohnKong/p/5887904.html
Copyright © 2011-2022 走看看