IIdea使用CXF开发WebService

写这篇文章主要是用于增强记忆,而我参考的是这位朋友的随笔,链接如下

http://www.xiaomager.com/415.html 

服务端开发过程

1.首先创建一个maven项目,如下图

IIdea使用CXF开发WebService_第1张图片

2.添加项目的依赖包以及设置相关配置

 提示:首先介绍一下基础环境 ,开发编译器 intellij idea ,我们的jdk是1.7,tomcat是7,spring使用的是spring4,cxf准备使用3.1.4,这里特别需要说明的是,cxf 3.0以后的版本只能在jdk1.7上使用,如果在1.6使用的话,会直接报错的。

<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.0modelVersion>

  <groupId>com.rainsoftgroupId>
  <artifactId>cxfWebserviceartifactId>
  <packaging>warpackaging>
  <version>1.0-SNAPSHOTversion>

  <name>cxfWebservicename>
  <url>http://maven.apache.orgurl>

  <properties>
      <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
      <java.version>1.7java.version>
      
      <spring.version>4.3.2.RELEASEspring.version>
      
      <cxf.version>3.1.4cxf.version>
  properties>

  <dependencies>
    
    
      <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-beansartifactId>
          <version>${spring.version}version>
      dependency>
      <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-webartifactId>
          <version>${spring.version}version>
      dependency>
      <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-coreartifactId>
          <version>${spring.version}version>
      dependency>

      
      <dependency>
          <groupId>org.apache.cxfgroupId>
          <artifactId>cxf-rt-frontend-jaxwsartifactId>
          <version>${cxf.version}version>
      dependency>
      <dependency>
          <groupId>org.apache.cxfgroupId>
          <artifactId>cxf-rt-transports-httpartifactId>
          <version>${cxf.version}version>
      dependency>
      <dependency>
          <groupId>org.apache.cxfgroupId>
          <artifactId>cxf-rt-frontend-jaxrsartifactId>
          <version>${cxf.version}version>
      dependency>

  dependencies>
  <build>
      <finalName>cxfWebservicefinalName>
      <defaultGoal>compiledefaultGoal>
      <resources>
          <resource>
              <directory>src/main/resourcesdirectory>
              
              <excludes>
                  
                  <exclude>conf/test/*exclude>
                  
                  <exclude>conf/production/*exclude>
                  
                  <exclude>conf/development/*exclude>
              excludes>
          resource>
          <resource>
              <directory>src/main/resources/conf/${profiles.active}directory>
              <targetPath>conftargetPath>
          resource>
      resources>
      <plugins>
          
          <plugin>
              <groupId>org.apache.maven.pluginsgroupId>
              <artifactId>maven-compiler-pluginartifactId>
              <version>3.3version>
              <configuration>
                  <source>${java.version}source>
                  <target>${java.version}target>
                  <showWarnings>trueshowWarnings>
                  <encoding>${project.build.sourceEncoding}encoding>
                  <compilerArguments>
                      <verbose />
                      <bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jarbootclasspath>
                  compilerArguments>
              configuration>
          plugin>
          
          <plugin>
              <groupId>org.apache.maven.pluginsgroupId>
              <artifactId>maven-surefire-pluginartifactId>
              <version>2.7.2version>
              <configuration>
                  <forkMode>onceforkMode>
                  <argLine>-Dfile.encoding=UTF-8argLine>
                  <skipTests>trueskipTests>
              configuration>
          plugin>
      plugins>

  build>

  <distributionManagement>
      <repository>
          <id>nexus-releasesid>
          <url>http://192.168.1.22:8081/nexus/content/repositories/releases/url>
      repository>
      <snapshotRepository>
          <id>nexus-snapshotsid>
          <url>http://192.168.1.22:8081/nexus/content/repositories/releases/url>
      snapshotRepository>
  distributionManagement>

  <profiles>
    
    <profile>
          <id>productionid>
          <properties>
              <profiles.active>productionprofiles.active>
          properties>
      profile>
      
      <profile>
          <id>testid>
          <properties>
              <profiles.active>testprofiles.active>
          properties>
      profile>

      <profile>
          <id>developmentid>
          <properties>
              <profiles.active>developmentprofiles.active>
          properties>
          <activation><activeByDefault>trueactiveByDefault>activation>
      profile>
  profiles>
project>

保存后,需要执行maven 的 clean, install 来下载jar包到maven的本地库。

 3.添加配置文件支持

一共有三处配置文件需要更新

第一处:需要新增一个cxf的配置文件,这里取名为cxf-servlet.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:jaxws="http://cxf.apache.org/jaxws"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://cxf.apache.org/jaxws
       http://cxf.apache.org/schemas/jaxws.xsd">

    <context:component-scan base-package="com.daisy" />
    <bean id="cxfdemo" class="com.daisy.cxf.server.impl.MyCxfServerImpl">
    bean>
    <jaxws:endpoint id="cxfService" implementor="#cxfdemo" address="/cxfserver" />
beans>

此处需要根据自己的包以及配置文件,添加配置

这里面主要就是定义了一个cxfService,它的 实现类是com.daisy.cxf.server.impl.CxfServerImpl,这个 实现类我们在第三步骤来加上,其次还定义了一个/cxfserver的路径,即我们的cxfserver服务端的请求路径。

 第二处:需要在spring主配置applicationContext文件里把这个新建的文件添加上,配置如下:

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
               xmlns:context="http://www.springframework.org/schema/context"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config />

    <import resource="cxf-servlet.xml" />

beans>

第三处:就是需要在web.xml里面配置cxf的servlet,具体如下:

该处定义了请求webService的前缀,及通过引入一个org.apache.cxf.transport.servlet.CXFServlet的servlet来处理 所有前缀 /webService/*的请求,所以 我们的cxf的全路径应该是 这里的servlet-mapping加上第一处的address,即  /webService/cxfserver。

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"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>cxfWebservicedisplay-name>

  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:conf/applicationContext.xmlparam-value>
  context-param>
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>
  listener>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  <servlet>
    <servlet-name>CXFServletservlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServletservlet-class>
    <load-on-startup>1load-on-startup>
  servlet>

  <servlet-mapping>
    <servlet-name>CXFServletservlet-name>
    <url-pattern>/webService/*url-pattern>
  servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.jspwelcome-file>
  welcome-file-list>
web-app>

 

 4. 添加cxf服务端的接口类和接口实现类,目录结构如下

 IIdea使用CXF开发WebService_第2张图片

java实现代码为 IMyCxfServer.java

此处必须声明一下,在接口的上面也必须加上 @WebService , 我开始抱着试试的心理将它去掉了,结果在客户端调用的时候就怎么都找不到对应的方法了

package com.daisy.cxf.server;

import javax.jws.WebService;

/**
 * @author daisy
 * @description IMyCxfServer
 * @date 2017/4/8
 */
@WebService
public interface IMyCxfServer {
    String sayHello(String name);
}

 

 

 MyCxfServerImpl.java 

package com.daisy.cxf.server.impl;

import com.daisy.cxf.server.IMyCxfServer;

import javax.jws.WebService;

/**
 * @author daisy
 * @description MyCxfServerImpl
 * @date 2017/4/8
 */
@WebService
public class MyCxfServerImpl implements IMyCxfServer {
    @Override
    public String sayHello(String name) {
        return "hello "+name;
    }
}

以上代码表示的意思很明白,即服务端提供一个sayHello的方法,将客户端传递的字符串参数 前面加上 hello 后返回。

 

5. 添加cxf服务端的接口类和接口实现类,目录结构如下

将项目添加到tomcat服务器上运行,访问链接 http://localhost:9090/daisyCxf//webService/cxfserver?wsdl 即可看到结果,如下图所示

 IIdea使用CXF开发WebService_第3张图片

 

转载于:https://www.cnblogs.com/qianna-daisy/p/6681970.html

你可能感兴趣的:(IIdea使用CXF开发WebService)