精华内容
参与话题
问答
  • CXFServlet

    2014-03-10 11:06:43
    package org.apache.cxf.transport.servlet; import java.io.IOException; import java.io.InputStream; import java.net.URL; import javax.servlet.ServletConfig; import javax.servlet.......
    package org.apache.cxf.transport.servlet;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletContext;
    import org.apache.cxf.Bus;
    import org.apache.cxf.BusFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.core.io.Resource;
    import org.springframework.web.context.support.WebApplicationContextUtils;
    import org.springframework.web.context.support.XmlWebApplicationContext;
    
    public class CXFServlet extends CXFNonSpringServlet
    {
      protected void loadBus(ServletConfig sc)
      {	//获取servlet信息
        ApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(sc.getServletContext());
    	//默认找web.xml配置文件中的"config-location"
    	
    	//<!-- 配置CXF的核心Servlet -->
    	//<servlet>
    	//	<servlet-name>cxf</servlet-name>
    	//	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    	//	<!-- 通过初始化参数指定cxf配置文件的位置 -->
    	//	<!-- 以下不注释则调用服务时会重新加载 -->
    	//	<!-- 
    	//	<init-param>
    	//		<param-name>config-location</param-name>
    	//		<param-value>classpath:cxf-servlet.xml</param-value>
    	//	</init-param>
    	//	 -->
    	//</servlet>
    	
        String configLocation = sc.getInitParameter("config-location");
        if (configLocation == null)
          try {
    		//当找不到默认的"config-location"时,去找cxf-servlet.xml文件
            InputStream is = sc.getServletContext().getResourceAsStream("/WEB-INF/cxf-servlet.xml");
            if ((is != null) && (is.available() > 0)) {
              is.close();
              configLocation = "/WEB-INF/cxf-servlet.xml";
            }
          }
          catch (Exception ex)
          {
          }
        if (configLocation != null) {
          wac = createSpringContext(wac, sc, configLocation);
        }
        if (wac != null)
          setBus((Bus)wac.getBean("cxf", Bus.class));
        else
          setBus(BusFactory.newInstance().createBus());
      }
    
      private ApplicationContext createSpringContext(ApplicationContext ctx, ServletConfig sc, String location)
      {
        XmlWebApplicationContext ctx2 = new XmlWebApplicationContext();
        ctx2.setServletConfig(sc);
    
        Resource r = ctx2.getResource(location);
        try {
          InputStream in = r.getInputStream();
          in.close();
        }
        catch (IOException e) {
          r = ctx2.getResource("classpath:" + location);
          try {
            r.getInputStream().close();
          }
          catch (IOException e2) {
            r = null;
          }
        }
        try {
          if (r != null)
            location = r.getURL().toExternalForm();
        }
        catch (IOException e)
        {
        }
        if (ctx != null) {
          ctx2.setParent(ctx);
          String[] names = ctx.getBeanNamesForType(Bus.class);
          if ((names == null) || (names.length == 0)) {
            ctx2.setConfigLocations(new String[] { "classpath:/META-INF/cxf/cxf.xml", location });
          }
          else
            ctx2.setConfigLocations(new String[] { location });
        }
        else {
          ctx2.setConfigLocations(new String[] { "classpath:/META-INF/cxf/cxf.xml", location });
        }
    
        ctx2.refresh();
        return ctx2;
      }
    }

     

    展开全文
  • 简单的cxfServlet实例

    万次阅读 2016-07-05 17:57:01
    简单的cxfServlet实例1 什么是cxfServletCXF implements the JAX-WS APIs which make building web services easy. 即是apache的一个webservice引擎。2 依赖的jar包spring4依赖的包 <groupId>org.springframework...

    简单的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
    浏览器结果
    这里写图片描述
    控制台输出,上面为加参数访问结果
    这里写图片描述

    展开全文
  • ssh学习笔记二:cxfServlet简单实现

    千次阅读 2016-07-05 17:52:43
    1 spring配置学习1.1 配置bean学习一个例子。cxfServlet简单实例,cxfServlet配置,访问cxfServlet配置下的项目,cxfServlet参数配置,cxfServlet返回结果配置

    1 spring配置学习

    1.1 配置bean

    学习一个例子。测试配置bean,配置两个family,family1和family2都有相同的属性但是属性值不同,加入配置文件,可以配置如下:

    1.1.1 配置bean的属性值spring-base.xml

    <!-- 使用class属性指定类的默认构造方法创建实例 名称由id属性指定  加上scope="prototype" 表示每次生成新的实例-->
        <bean id="family" class="com.study.spring.Family" >
            <property name="mother" value="Lily"></property>
            <property name="father" value="Bob"></property>
            <property name="sister" value="Jerry"></property>
            <property name="brother" value="Ham"></property>
        </bean>
        <bean id="familyUtil1" class="com.study.spring.FamilyUtil">
            <property name="family" ref="family"></property>
        </bean>
        <bean id="family2" class="com.study.spring.Family" >
            <property name="mother" value="family2-Lily"></property>
            <property name="father" value="family2-Bob"></property>
            <property name="sister" value="family2-Jerry"></property>
            <property name="brother" value="family2-Ham"></property>
        </bean>
        <bean id="familyUtil2" class="com.study.spring.FamilyUtil">
            <property name="family" ref="family2"></property>
        </bean>

    1.1.2 Family是个实体类

    package com.study.spring;
    
    public class Family{
        private String mother;
        private String father;  
        private String sister;  
        private String brother;
        public String getMother() {
            return mother;
        }
        public void setMother(String mother) {
            this.mother = mother;
        }
        public String getFather() {
            return father;
        }
        public void setFather(String father) {
            this.father = father;
        }
        public String getSister() {
            return sister;
        }
        public void setSister(String sister) {
            this.sister = sister;
        }
        public String getBrother() {
            return brother;
        }
        public void setBrother(String brother) {
            this.brother = brother;
        }   
    }

    1.1.3 FamilyUtil类,包含family属性

    package com.study.spring;
    
    public class FamilyUtil {
        private Family family = new Family();
        public Family getFamily() {
            return family;
        }
        public void setFamily(Family family) {
            this.family = family;
        }
        public String listFamily() {
            if(family.getMother() != null){
                System.out.println(family.getMother());
                System.out.println(family.getFather());
                System.out.println(family.getBrother());
                System.out.println(family.getSister());
                return "success";
            }else{
                System.out.println("为空");
                return "fail";
            }       
        }
    }

    1.1.4 测试类TestFamily

    package com.study.spring;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Controller;
    
    import com.study.utils.struts.BaseAction;
    @Controller("testFamily")
    @Scope("prototype")
    public class TestFamily extends BaseAction{
    
        /** 
        * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么) 
        */ 
        private static final long serialVersionUID = 1L;
        @Autowired
        @Qualifier("familyUtil1")
        private FamilyUtil familyUtil1;
    
        @Autowired
        @Qualifier("familyUtil2")
        private FamilyUtil familyUtil2;
    
        public void listFamily(){
            if("success".equals(familyUtil1.listFamily())){
                writeJson2Page("success");
            }else{
                writeJson2Page("fail");
            }
        }
    
        public void listFamily2(){
            if("success".equals(familyUtil2.listFamily())){
                writeJson2Page("success");
            }else{
                writeJson2Page("fail");
            }
        }
    }

    1.1.5 调用方法

    调用TestFamily.listFamily()时,控制台打印结果

    Lily
    Bob
    Ham
    Jerry

    调用TestFamily.ListFamily2()时,控制台打印结果

    family2-Lily
    family2-Bob
    family2-Ham
    family2-Jerry

    结果与预期的一样。

    1.1.6 结果分析

    TestFamily通过注解的方式加载bean “familyUtil1”,在spring-base.xml配置文件中

    即,familyUtil1有family属性,且指向bean “family”,而bean “family”又有以下属性

            <property name="mother" value="Lily"></property>
            <property name="father" value="Bob"></property>
            <property name="sister" value="Jerry"></property>
            <property name="brother" value="Ham"></property>

    故调用familyUtil1.listFamily()时会显示配置文件的family属性配置。调用familyUtil2.listFamily同理会显示family2的属性配置。

    2 简单的cxfServlet实例

    2.1 什么是cxfServlet

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

    2.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>   

    2.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>

    2.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>

    2.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";
        }
    }
    

    2.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
    浏览器结果
    这里写图片描述
    控制台输出,上面为加参数访问结果
    这里写图片描述

    展开全文
  • 1、错误描述严重: Servlet /FlashProject threw load() ...java.lang.ClassNotFoundException: org.apache.cxf.transport.servlet.CXFServlet at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappCl

    1、错误描述

    严重: Servlet /FlashProject threw load() exception
    java.lang.ClassNotFoundException: org.apache.cxf.transport.servlet.CXFServlet
    	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    	at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:506)
    	at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:488)
    	at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:115)
    	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1148)
    	at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
    	at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5231)
    	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5518)
    	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    	at org.apache.catalina.core.StandardContext.reload(StandardContext.java:4009)
    	at org.apache.catalina.startup.HostConfig.reload(HostConfig.java:1479)
    	at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1462)
    	at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1646)
    	at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:328)
    	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
    	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    	at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1374)
    	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1546)
    	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1556)
    	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1524)
    	at java.lang.Thread.run(Thread.java:745)

    2、错误原因

         在web.xml中添加了一段:

    <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>
        但是没有导入有关cxf的jar,导致报错


    3、解决办法

         将cxf-2.6.0.jar拷贝到Java Web项目中的web/lib目录下,重新启动下Tomcat 

    展开全文
  • 更改或隐藏org.apache.cxf.transport.servlet.CXFServlet的rest或soap服务列表
  • 关于CXFServlet的应用

    千次阅读 2015-04-20 23:34:31
    一、简介: 在开发java webservice时,有两个比较流行的框架:axis2和cxf。cxf可以无缝的和spring集成,而axis2需要打包成aar文件,在tomcat下面跑。Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 ...
  • jdk1.6中的架包和cxf架包冲突,修改jdk版本即可,亲测修改为jdk1.8有效
  • 环境 JDK15 WAS6.1.0.21 ...[09-11-3 14:34:59:238 CST] 00000051 ServletWrappe E SRVE0068E: 未捕获到 servlet CXFServlet 的其中一个服务方法中抛出的异常。抛出的异常:java.lang.Incomp...
  • 1.项目中本来集成有CXF Webservice服务,springboot的parent版本号为2.0.2.RELEASE,集成的CXF版本号为: <dependency> <groupId>org.apache.cxf</groupId>...cxf-spring-boot-starter-jaxws<...

空空如也

1 2 3 4 5 ... 12
收藏数 239
精华内容 95
关键字:

cxfservlet