简单的cxfServlet实例

Posted RunningFan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单的cxfServlet实例相关的知识,希望对你有一定的参考价值。

简单的cxfServlet实例

1 什么是cxfServlet

CXF implements the JAX-WS APIs which make building web services easy.
即是apache的一个webservice引擎。

2 依赖的jar包

spring4依赖的包

 <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>

cxf3依赖的jar包

<dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-core</artifactId>
        <version>3.0.0-milestone2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxrs</artifactId>
        <version>3.0.0-milestone2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-extension-providers</artifactId>
        <version>3.0.0-milestone2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-rs-client</artifactId>
      <version>3.0.0-milestone1</version>
    </dependency>   

3 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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>gyh</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
  </welcome-file-list>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:*.xml</param-value>
  </context-param>
  <!-- CXF implements the JAX-WS APIs which make building web services easy.apache的一个webservice引擎。 -->
    <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>/service/*</url-pattern>
  </servlet-mapping>

   <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener> 
</web-app>

4 spring-cxf.xml加入配置

<import resource="classpath:META-INF/cxf/cxf.xml"/>
  <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

  <bean id="helloService" class="com.study.spring.HelloServiceImpl"></bean>

  <jaxrs:server id="defaultService" address="/">
       <jaxrs:serviceBeans>
             <ref bean="helloService"/>
       </jaxrs:serviceBeans>
  </jaxrs:server>

5 代码如下

接口类IHelloService

package com.study.spring;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.study.utils.spring.IBaseAction;

@Path("/hello")
public interface IHelloService extends IBaseAction
    @GET
    @Path("/sayHello")
    @Produces(MediaType.TEXT_HTML,MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON)
    public String sayHello(@FormParam("msg")String msg); 

接口实现类HelloServiceImpl

package com.study.spring;

public class HelloServiceImpl implements IHelloService

    @Override
    public String sayHello(String msg) 
        System.out.println("Hello World!" + msg);
        return "success";
    

6 访问接口

启动tomcat,访问地址由tomcat的地址+项目名称(tomcat下的server.xml可以修改)+web.xml中cxfServlet配置的 地址+spring-cxf.xml中配置的address+interface配置path
如我的ip地址 192.168.100.115:8081,项目名gyh,
web.xml配置 “/service”,
spring-cxf.xml配置 “/”
类Path: “/hello”
方法Path: “/sayHello”
则访问地址 :http://192.168.100.115:8081/gyh/service/hello/sayHello
加参数访问:http://192.168.100.115:8081/gyh/service/hello/sayHello?msg=Sweet
浏览器结果

控制台输出,上面为加参数访问结果

以上是关于简单的cxfServlet实例的主要内容,如果未能解决你的问题,请参考以下文章

So easy Webservice 8.spring整合CXF 发布WS

有没有办法让 org.apache.cxf.transport.servlet.CXFServlet 与 Jetty 11 一起使用

webservice 项目 配置wsdl

spring cxf 配置步骤

webservice框架cxf

web.xml中配置webService的CXFService