java的WebService實踐(cxf)

Java發佈WebService,結合Spring,經過cxf的方式html

難點:一、引用什麼jar包;java

  

 

一、建立接口web

源碼以下:spring

package com.nankang;

import javax.jws.WebParam;
import javax.jws.WebService;


@WebService
public interface HelloWorld {
    String sayHi(@WebParam(name="text") String text);
}

 

二、實現接口apache

源碼以下:app

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;
    }

}

 

三、web.xml的配置url

源碼以下:spa

<?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>

 

四、添加applicationContext.xmlcode

源碼以下:component

<?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>

 

五、訪問

http://localhost:8080/WebServiceTest/webservice/helloWorld?wsdl

六、訪問源碼

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"));
    }
}

七、發佈示例:

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

相關文章
相關標籤/搜索