java简单开发cxf webService样例

2018-06-27 09:47:21来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

最近学了cxf框架开发webservice,简单搭了个接口,方便后续翻阅,本人才疏学浅,若有不足,请多多谅解!

一、服务端:

1.所用到的jar包:
  

maven的pom.xml配置:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.xiamenair.CXFWebService</groupId>
  <artifactId>cxf-demo</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>cxf-demo Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <!-- 添加 Spring dependency -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-tx</artifactId>
         <version>4.1.7.RELEASE</version>
        </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
     
    <!-- 添加CXF dependency  -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-core</artifactId>
        <version>3.1.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>3.1.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>3.1.5</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>cxf-demo</finalName>
  </build>
</project>

 

2.配置web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    <display-name>cxf-demo</display-name>
    
    <!-- Spring config-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext*.xml</param-value>
    </context-param>
      
    <!-- Spring listener -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Add on a servlet to handle web service request -->
    <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>/webservice/*</url-pattern> 
    </servlet-mapping>
</web-app>

3.配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd  
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/cache 
    http://www.springframework.org/schema/cache/spring-cache-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-servlet.xml" />
    <bean id="helloWordServiceImpl" class="com.xmal.webservice.HelloWordServiceImpl"></bean>
    <jaxws:endpoint id="helloWordWebService" implementor="#helloWordServiceImpl" address="/helloWordWebService"></jaxws:endpoint>
    <!-- 自动扫描包,以便注册带有@Service、@repository、@Component等注解的类成为spring的bean -->
    <context:component-scan base-package="com.xmal.javafw" annotation-config="true" >
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        <!-- <context:exclude-filter type="regex" expression=".temp.*"/> -->
    </context:component-scan>
</beans>

4.建一个service:HelloWordService

package com.xmal.webservice;

import javax.jws.WebService;

@WebService
public interface HelloWordService {
    
    public String sayHello(String name);
}

以及他的实现类:

package com.xmal.webservice;

import javax.jws.WebService;

import org.springframework.stereotype.Component;

@Component("helloWorld")
@WebService
public class HelloWordServiceImpl implements HelloWordService {

    public String sayHello(String name) {
        return "Hello Word,"+name;
    }

}

服务端就已经开发完毕,部署tomcat,访问:http://localhost:8080/cxf-demo/webService/helloWordService?wsdl,地址根据自己配置的为准,如果可以访问,就说明成功。

二、客户端

建立客户端

JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        org.apache.cxf.endpoint.Client client = dcf.createClient("xxxx?wsdl");
        
        Object[] objects;
        try {
            objects = client.invoke("方法名", "请求内容");
            System.out.println(objects[0].toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Spring的注解问题

下一篇:基于SpringMVC+Spring+MyBatis实现秒杀系统【数据库接口】