zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Linux性能监测:磁盘IO篇
    Linux性能监测:网络篇
    Linux性能监测:内存篇
    Linux系统和性能监控之CPU篇
    linux 进程和线程讲解
    web-单机时代
    自动化-ELK日志管理
    自动化--zabbix-proxy
    自动化-zabbix(2)
    IOS中的用户安全
  • 原文地址:https://www.cnblogs.com/sshoub/p/3855719.html
Copyright © 2011-2022 走看看