使用CXF和spring搭建webService服务

简介: 虽然下一个项目需要使用xfire,但是在查资料的过程中还是看到有不少地方都说cxf比xfire更好,cxf继承了xfire,但是不仅仅包含xfire,因此便也一起来尝试尝试。大概是有了xfire的经验吧,cxf的搭建比xfire快了许多。cxf的许多参数感觉和xfire差不多,因此便不做太多的解释,如果不明白的可以参考之前的xfire搭建来促进理解。搭建过程如下:1、使用eclipse创
虽然下一个项目需要使用xfire,但是在查资料的过程中还是看到有不少地方都说cxf比xfire更好,cxf继承了xfire,但是不仅仅包含xfire,因此便也一起来尝试尝试。大概是有了xfire的经验吧,cxf的搭建比xfire快了许多。

cxf的许多参数感觉和xfire差不多,因此便不做太多的解释,如果不明白的可以参考之前的xfire搭建来促进理解。

搭建过程如下:
1、使用eclipse创建一个maven项目,maven导包的pom.xml的整体内容如下:
  <modelVersion>4.0.0</modelVersion>
  <groupId>cxfTest</groupId>
  <artifactId>cxfTest</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>cxfTest Maven Webapp</name>
  <url> http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-core</artifactId>
        <version>3.0.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>3.0.4</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>cxfTest</finalName>
  </build>
</project>

2、创建webService服务类的接口:
package cxfTest;
import javax.jws.WebService;

@WebService(serviceName = "CxfTestService", targetNamespace = " http://cxfTest")
public interface CxfTestService {
    public String getUserName();
}

3、实现类代码,@Service实际上是spring注解:
package cxfTest.cxfTestImp;
import javax.jws.WebService;
import org.springframework.stereotype.Service;
import cxfTest.CxfTestService;

@Service("CxfTestService")
@WebService(targetNamespace = " http://cxfTest")
public class CxfTestServiceImp implements CxfTestService {

    @Override
    public String getUserName() {

        return "name:cxfTest";
    }
}

4、cxf配置文件:
<?xml version="1.0" encoding="UTF-8"?> 
xmlns:jaxws=" http://cxf.apache.org/jaxws
                     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                     http://www.springframework.org/schema/context 
                     http://cxf.apache.org/jaxws  
                     http://cxf.apache.org/schemas/jaxws.xsd">  

    <context:component-scan base-package="cxfTest"/> 

   <jaxws:server id="getUserName"
        serviceClass="cxfTest.CxfTestService"
        address="/CxfTestService">
        <jaxws:serviceBean>
            <bean
                class="cxfTest.cxfTestImp.CxfTestServiceImp"></bean>
        </jaxws:serviceBean>
    </jaxws:server>
</beans> 

这里需要注意的是,一般实际应用时可能会加上下边的代码,这两每当有客户端访问的时候都会把相关的日志打印出来:
<jaxws:inInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
        </jaxws:inInterceptors>
        <jaxws:outInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
        </jaxws:outInterceptors>
这段代码加在 <jaxws:server>和</jaxws:server>中间。

5、web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>

     <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>classpath:cxf-context.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>/services/*</url-pattern> 
      </servlet-mapping> 
</web-app>

6、把项目加入到tomcat中启动,然后在浏览器访问 http://localhost:8082/cxfTest/services,看到页面如下:



7、页面正常访问后,我新建一个一个项目,导入了服务端导入的所有的jar包,然后写了个简单的main方法进行测试。需要注意的是,跟xfire一样,这里也需要写一个和服务端一模一样的服务接口(包路径可以不一样),如:
package cxfTest1;
import javax.jws.WebService;
@WebService(serviceName = "CxfTestService", targetNamespace = " http://cxfTest")
public interface CxfTestService {
    public String getUserName();
}

然后对应的模拟客户端调用代码如下:
package cxfTest1;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class CxfTest {

    public static void main(String[] args) {
        JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
        // factoryBean.getInInterceptors().add(new LoggingInInterceptor());
        // factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
        factoryBean.setServiceClass(CxfTestService.class);
        factoryBean
                .setAddress(" http://localhost:8082/cxfTest/services/CxfTestService");
        CxfTestService impl = (CxfTestService) factoryBean.create();
        System.out.println(impl.getUserName());

    }
}

上边被注释的代码也是日志相关的,加不加都不影响主要业务功能,可以自己选择用还是不用。执行main方法后控制台输出如下:



目录
相关文章
|
17天前
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
53 7
|
6月前
|
缓存 NoSQL Java
【Azure Redis 缓存】示例使用 redisson-spring-boot-starter 连接/使用 Azure Redis 服务
【Azure Redis 缓存】示例使用 redisson-spring-boot-starter 连接/使用 Azure Redis 服务
100 0
|
6月前
【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值
【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值
|
3月前
【Azure App Service】PowerShell脚本批量添加IP地址到Web App允许访问IP列表中
Web App取消公网访问后,只允许特定IP能访问Web App。需要写一下段PowerShell脚本,批量添加IP到Web App的允许访问IP列表里!
|
5月前
|
Java API 对象存储
微服务魔法启动!Spring Cloud与Netflix OSS联手,零基础也能创造服务奇迹!
这段内容介绍了如何使用Spring Cloud和Netflix OSS构建微服务架构。首先,基于Spring Boot创建项目并添加Spring Cloud依赖项。接着配置Eureka服务器实现服务发现,然后创建REST控制器作为API入口。为提高服务稳定性,利用Hystrix实现断路器模式。最后,在启动类中启用Eureka客户端功能。此外,还可集成其他Netflix OSS组件以增强系统功能。通过这些步骤,开发者可以更高效地构建稳定且可扩展的微服务系统。
94 1
|
6月前
|
关系型数据库 MySQL Linux
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
|
6月前
|
Shell PHP Windows
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
|
6月前
|
存储 Java Spring
【Azure Spring Cloud】Azure Spring Cloud服务,如何获取应用程序日志文件呢?
【Azure Spring Cloud】Azure Spring Cloud服务,如何获取应用程序日志文件呢?
|
6月前
|
Linux 应用服务中间件 网络安全
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
|
6月前
|
Java Spring
【Azure 服务总线】Spring Cloud 的应用 使用Service Bus 引起 org.springframework.beans.BeanInstantiationException 异常,无法启动
【Azure 服务总线】Spring Cloud 的应用 使用Service Bus 引起 org.springframework.beans.BeanInstantiationException 异常,无法启动

热门文章

最新文章