欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java简单开发cxf webService样例

程序员文章站 2023-11-16 10:22:34
最近学了cxf框架开发webservice,简单搭了个接口,方便后续翻阅,本人才疏学浅,若有不足,请多多谅解! 一、服务端: 1.所用到的jar包: maven的pom.xml配置: 2.配置web.xml: 3.配置applicationContext.xml 4.建一个service:Hello ......

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

一、服务端:

1.所用到的jar包:
  java简单开发cxf webService样例

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