基於spring4.3.6,cxf3.0.12的WebService開發

基於spring4.3.6,cxf3.0.12的WebService開發html

因爲項目使用Spring開發,因此筆者選擇了Apache CXF進行WebService開發,緣由是Apache CXF 提供方便的Spring整合方法,
能夠經過註解、Spring標籤式配置來暴露Web Services和消費Web Services。
1.    首先去http://cxf.apache.org/download.html 下載最新的版本(目前是3.0.12)
2.    Maven pom依賴相應的包到項目中,大概包以下:
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.0.12</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.0.12</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.6.RELEASE</version>
</dependency>
3.    新建一個接口

       @WebServicejava

public interface HelloWorldService {web

              public String getNewName(String userName);spring

}
使用@WebService標識讓CXF知道使用該接口來建立WSDL
4.    新建一個實現類

       @WebService(endpointInterface="org.app.demo.spring.service.HelloWorldService")apache

public class HelloWorldServiceImpl implements HelloWorldService {tomcat

              public String getNewName(String userName) {服務器

                     return "Hello Spring!" + userName;mvc

              }app

}
5.    修改相應的配置文件
ApplicationContext.xml中加入以下代碼

<jaxws:endpointfrontend

         id="helloWorld"

         implementor="org.app.demo.spring.service.impl.HelloWorldServiceImpl"

           address="/HelloWorld" />
或者
<bean id=" helloWorldService" class=" org.app.demo.spring.service.impl.HelloWorldServiceImpl" />

<jaxws:endpoint

         id="helloWorld"

         implementor="#helloWorldService"

           address="/HelloWorld" />
 
注意:XML頭文件需相應添加
xmlns:jaxws="http://cxf.apache.org/jaxws
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
 
web.xml中加入以下代碼

<servlet>

              <servlet-name>CXFServlet</servlet-name>

              <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

              <load-on-startup>2</load-on-startup>

       </servlet>

 

       <servlet-mapping>

              <servlet-name>CXFServlet</servlet-name>

              <url-pattern>/*</url-pattern>

</servlet-mapping>
6.    發佈項目到tomcat 服務器。
7.    啓動Tomcat,打開http://localhost:8080/myapp/HelloWorld?wsdl就能夠看到了
8.    客戶端調(java project)用也很方便,以下代碼:

       URL wsdlUrl = new URL("http://192.168.0.114:8080/myapp/HelloWorld?wsdl");

       QName SERVICE_NAME = new QName("http://impl.service.spring.demo.app.org/", "HelloWorldServiceImplService");

       Service service = Service.create(wsdlUrl, SERVICE_NAME);

       HelloWorldService hello = service.getPort(HelloWorldService.class);

System.out.println(hello.getNewName("WebService調用"));
8.客戶端調(java web) ApplicationContext.xml中加入以下代碼

<bean id="helloWorld"

class="org.app.demo.spring.service.HelloWorldService"

      factory-bean="clientFactory" factory-method="create"/>

   

    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">

      <property name="serviceClass" value=" org.app.demo.spring.service.HelloWorldService"/>

      <property name="address" value=" http://192.168.0.114:8080/myapp/HelloWorld "/>

     </bean>

類 @Autowired

     private HelloWorldService helloWorld;
而後執行代碼後,會打印出 Hello Spring!WebService調用。
注意   1. IP地址爲Webservice的服務器的地址。
         2. 必須把相應的接口複製到客戶端項目中。
相關文章
相關標籤/搜索