OSGI:使用Karaf JAX-RS的CXF异常:没有找到资源的资源方法

时间:2017-01-06 21:04:19

标签: rest jax-rs osgi cxf apache-karaf

我在使用OSGI和Karaf创建简单的CXF Web服务时遇到异常。

非常感谢任何有关修复的意见/建议。

以下是我的项目说明和我遵循的步骤:

  1. 下载了Apache Karaf-4.0.7
  2. 在命令行中使用项目路径
  3. 运行“mvn clean install”
  4. 启动Karaf控制台,并运行以下命令

           feature:repo-add cxf 3.1.8
           feature:install cxf/3.1.8
    
  5. 接口:

      public interface MyRestService { 
             String pingMe(String echo);
        }
    

    实现:

    @Path("/")
    public class MyRestServiceImpl implements MyRestService {
    
        @GET
        @Path("/{echo}")
        @Produces(MediaType.APPLICATION_XML)
        public String pingMe(@PathParam("echo") String echo) {
            return "Knock Knock: " + echo + " !!";
        }
    }
    

    OSGI blueprint.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
        xmlns:cxf="http://cxf.apache.org/blueprint/core" xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs"
        xmlns:jaxws="http://cxf.apache.org/blueprint/jaxws"
        xsi:schemaLocation="
          http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
          http://www.osgi.org/xmlns/blueprint-ext/v1.1.0 https://svn.apache.org/repos/asf/aries/tags/blueprint-0.3.1/blueprint-core/src/main/resources/org/apache/aries/blueprint/ext/blueprint-ext.xsd  
          http://cxf.apache.org/blueprint/jaxws http://cxf.apache.org/schemas/blueprint/jaxws.xsd
          http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd
          http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
          http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd
          http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd
        ">
        <cxf:bus id=”myBusId”>
            <cxf:features>
                <cxf:logging></cxf:logging>
            </cxf:features>
        </cxf:bus>
    
        <jaxrs:server id="myRestService" address="/RestProject/SimpleRestCall">
            <jaxrs:serviceBeans>
                <ref component-id="myRestImpl" />
            </jaxrs:serviceBeans>
        </jaxrs:server>
    
        <bean id="myRestImpl" class="com.rest.api.impl.MyRestServiceImpl" />  
    </blueprint>
    

    Features.xml

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <features xmlns="http://karaf.apache.org/xmlns/features/v1.4.0"
        name="restAPI">
    
        <repository>mvn:org.apache.cxf.karaf/apache-cxf/3.1.8/xml/features</repository> 
    
        <feature name="test" description="simple test" version="1.0.0-SNAPSHOT">
            <details>A Rest server</details>
    
            <!-- CXF and depdendencies -->
            <feature version="3.1.8">cxf-jaxrs</feature>
    
            <!-- Jackson and dependencies -->
            <bundle>mvn:org.apache.servicemix.specs/org.apache.servicemix.specs.jsr339-api-2.0/2.6.0</bundle>
            <bundle>mvn:com.github.fge/jackson-coreutils/1.8</bundle>
            <bundle>mvn:com.fasterxml.jackson.core/jackson-core/2.7.4</bundle>
            <bundle>mvn:com.fasterxml.jackson.jaxrs/jackson-jaxrs-base/2.7.4</bundle>
            <bundle>mvn:com.fasterxml.jackson.core/jackson-annotations/2.7.4</bundle>
            <bundle>mvn:com.fasterxml.jackson.core/jackson-databind/2.7.4</bundle>
            <bundle>mvn:com.fasterxml.jackson.jaxrs/jackson-jaxrs-json-provider/2.7.4</bundle>
            <bundle>mvn:com.github.fge/msg-simple/1.1</bundle>
            <bundle>mvn:com.google.guava/guava/16.0.1</bundle>
            <bundle>mvn:com.github.fge/btf/1.2</bundle>
            <bundle>wrap:mvn:com.google.code.findbugs/jsr305/2.0.1</bundle>
    
            <!-- The myRest Server -->
            <bundle>mvn:com.rest.ebb.test/myRest/1.0.0-SNAPSHOT</bundle>
    
        </feature>
    
    </features>
    

    的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>
    
        <parent>
            <groupId>com.rest.ebb.test</groupId>
            <artifactId>myRest-parent</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </parent>
    
        <groupId>com.rest.ebb.test</groupId>
        <artifactId>myRest</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    
        <packaging>bundle</packaging>
        <name>MyRestServer</name>
        <description>A server for test</description>
        <url>.example.com</url>
       <properties>
       <cxf.version>3.1.8</cxf.version>
        <skipTests>true</skipTests>
      </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>maven-bundle-plugin</artifactId>
                    <version>2.4.0</version>
                    <extensions>true</extensions>
                    <configuration>
                        <instructions>
                            <Bundle-Activator>com.ebb.rest.Activator</Bundle-Activator>
                            <Embed-Dependency>!org.osgi.core,*</Embed-Dependency>
                            <Embed-Transitive>true</Embed-Transitive>
                            <Import-Package>
                                !com.google.protobuf,!com.google.protobuf.*,
                                !com.jcraft.*,
                                !com.ning.*,
                                !groovy.lang,
                                !javassist,
                                !lzma.*,
                                !net.jpountz.*,
                                !org.apache.*,
                                !org.bouncycastle.*,
                                !org.codehaus.*,
                                !org.eclipse.*,
                                !org.jboss.*,
                                !rx,
                                !sun.security.*,
                                !net.bytebuddy.*,
                                !org.HdrHistogram,
                                !org.slf4j.event,
                                !org.xerial.*,
                                !sun.reflect,
                                !sun.misc,
                                !sun.util.calendar,
                                !com.sun.jdi.*,
                                !jersey.repackaged.com.google.common.*,
                                !javax.servlet,
                                *
                            </Import-Package>
                        </instructions>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
        <dependencies>
            <dependency>
                <groupId>org.osgi</groupId>
                <artifactId>org.osgi.core</artifactId>
                <version>5.0.0</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.core</groupId>
                <artifactId>jersey-server</artifactId>
                <version>2.24</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.containers</groupId>
                <artifactId>jersey-container-grizzly2-http</artifactId>
                <version>2.24</version>
            </dependency>
            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>19.0</version>
                <type>bundle</type>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-core</artifactId>
                <version>2.1.0</version>
            </dependency>
            <dependency>
                <groupId>com.google.inject</groupId>
                <artifactId>guice</artifactId>
                <version>4.1.0</version>
            </dependency>
            <dependency>
                <groupId>com.google.inject.extensions</groupId>
                <artifactId>guice-multibindings</artifactId>
                <version>4.1.0</version>
            </dependency>
            <dependency>
                <groupId>com.datastax.cassandra</groupId>
                <artifactId>cassandra-driver-core</artifactId>
                <version>3.1.0</version>
            </dependency>
            <dependency>
                <groupId>org.threeten</groupId>
                <artifactId>threetenbp</artifactId>
                <version>1.3.2</version>
            </dependency>
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>2.8.0</version>
            </dependency>
            <dependency>
                <groupId>com.opencsv</groupId>
                <artifactId>opencsv</artifactId>
                <version>3.7</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
                <version>1.7.21</version>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.2</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.5</version>
            </dependency>
             <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>jsr311-api</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>${cxf.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        </dependencies>
    </project>
    

    错误:

    我无法在Karaf中部署osgi包,并收到以下错误:

    2017-01-06 11:00:39,340 | WARN  | pool-9-thread-1  | ResourceUtils                    | 141 - org.apache.cxf.cxf-rt-frontend-jaxrs - 3.1.8 | No resource methods have been found for reso
     class com.rest.api.impl.MyRestServiceImpl
    2017-01-06 11:00:39,373 | ERROR | pool-9-thread-1  | AbstractJAXRSFactoryBean         | 141 - org.apache.cxf.cxf-rt-frontend-jaxrs - 3.1.8 | No resource classes found
    2017-01-06 11:00:39,375 | WARN  | pool-9-thread-1  | BeanRecipe                       | 33 - org.apache.aries.blueprint.core - 1.6.2 | Object to be destroyed is not an instance of Unwra
    edBeanHolder, type: null
    2017-01-06 11:00:39,385 | ERROR | pool-9-thread-1  | BlueprintContainerImpl           | 33 - org.apache.aries.blueprint.core - 1.6.2 | Unable to start blueprint container for bundle mvn:com.abb.ecc.my/myRest/1.0.0-SNAPSHOT 
    org.osgi.service.blueprint.container.ComponentDefinitionException: Unable to initialize bean myRestService
            at org.apache.aries.blueprint.container.BeanRecipe.runBeanProcInit(BeanRecipe.java:738)[33:org.apache.aries.blueprint.core:1.6.2]
            at org.apache.aries.blueprint.container.BeanRecipe.internalCreate2(BeanRecipe.java:848)[33:org.apache.aries.blueprint.core:1.6.2]
            at org.apache.aries.blueprint.container.BeanRecipe.internalCreate(BeanRecipe.java:811)[33:org.apache.aries.blueprint.core:1.6.2]
            at org.apache.aries.blueprint.di.AbstractRecipe$1.call(AbstractRecipe.java:79)[33:org.apache.aries.blueprint.core:1.6.2]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266)[:1.8.0_51]
            at org.apache.aries.blueprint.di.AbstractRecipe.create(AbstractRecipe.java:88)[33:org.apache.aries.blueprint.core:1.6.2]
            at org.apache.aries.blueprint.container.BlueprintRepository.createInstances(BlueprintRepository.java:255)[33:org.apache.aries.blueprint.core:1.6.2]
            at org.apache.aries.blueprint.container.BlueprintRepository.createAll(BlueprintRepository.java:186)[33:org.apache.aries.blueprint.core:1.6.2]
            at org.apache.aries.blueprint.container.BlueprintContainerImpl.instantiateEagerComponents(BlueprintContainerImpl.java:724)[33:org.apache.aries.blueprint.core:1.6.2]
            at org.apache.aries.blueprint.container.BlueprintContainerImpl.doRun(BlueprintContainerImpl.java:411)[33:org.apache.aries.blueprint.core:1.6.2]
            at org.apache.aries.blueprint.container.BlueprintContainerImpl.run(BlueprintContainerImpl.java:276)[33:org.apache.aries.blueprint.core:1.6.2]
            at org.apache.aries.blueprint.container.BlueprintExtender.createContainer(BlueprintExtender.java:300)[33:org.apache.aries.blueprint.core:1.6.2]
            at org.apache.aries.blueprint.container.BlueprintExtender.createContainer(BlueprintExtender.java:269)[33:org.apache.aries.blueprint.core:1.6.2]
            at org.apache.aries.blueprint.container.BlueprintExtender.createContainer(BlueprintExtender.java:265)[33:org.apache.aries.blueprint.core:1.6.2]
            at org.apache.aries.blueprint.container.BlueprintExtender.modifiedBundle(BlueprintExtender.java:255)[33:org.apache.aries.blueprint.core:1.6.2]
            at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.customizerModified(BundleHookBundleTracker.java:500)[43:org.apache.aries.util:1.1.1]
            at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.customizerModified(BundleHookBundleTracker.java:433)[43:org.apache.aries.util:1.1.1]
            at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$AbstractTracked.track(BundleHookBundleTracker.java:725)[43:org.apache.aries.util:1.1.1]
            at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.bundleChanged(BundleHookBundleTracker.java:463)[43:org.apache.aries.util:1.1.1]
            at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$BundleEventHook.event(BundleHookBundleTracker.java:422)[43:org.apache.aries.util:1.1.1]
            at org.apache.felix.framework.util.SecureAction.invokeBundleEventHook(SecureAction.java:1179)[org.apache.felix.framework-5.4.0.jar:]
            at org.apache.felix.framework.util.EventDispatcher.createWhitelistFromHooks(EventDispatcher.java:731)[org.apache.felix.framework-5.4.0.jar:]
            at org.apache.felix.framework.util.EventDispatcher.fireBundleEvent(EventDispatcher.java:486)[org.apache.felix.framework-5.4.0.jar:]
            at org.apache.felix.framework.Felix.fireBundleEvent(Felix.java:4541)[org.apache.felix.framework-5.4.0.jar:]
            at org.apache.felix.framework.Felix.startBundle(Felix.java:2172)[org.apache.felix.framework-5.4.0.jar:]
            at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:998)[org.apache.felix.framework-5.4.0.jar:]
            at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:984)[org.apache.felix.framework-5.4.0.jar:]
            at org.apache.karaf.features.internal.service.FeaturesServiceImpl.startBundle(FeaturesServiceImpl.java:1286)[10:org.apache.karaf.features.core:4.0.7]
            at org.apache.karaf.features.internal.service.Deployer.deploy(Deployer.java:846)[10:org.apache.karaf.features.core:4.0.7]
            at org.apache.karaf.features.internal.service.FeaturesServiceImpl.doProvision(FeaturesServiceImpl.java:1176)[10:org.apache.karaf.features.core:4.0.7]
            at org.apache.karaf.features.internal.service.FeaturesServiceImpl$1.call(FeaturesServiceImpl.java:1074)[10:org.apache.karaf.features.core:4.0.7]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266)[:1.8.0_51]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)[:1.8.0_51]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)[:1.8.0_51]
            at java.lang.Thread.run(Thread.java:745)[:1.8.0_51]
    Caused by: org.apache.cxf.service.factory.ServiceConstructionException
            at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:219)
            at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.init(JAXRSServerFactoryBean.java:142)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.8.0_51]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)[:1.8.0_51]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.8.0_51]
            at java.lang.reflect.Method.invoke(Method.java:497)[:1.8.0_51]
            at org.apache.aries.blueprint.utils.ReflectionUtils.invoke(ReflectionUtils.java:299)[33:org.apache.aries.blueprint.core:1.6.2]
            at org.apache.aries.blueprint.container.BeanRecipe.invoke(BeanRecipe.java:980)[33:org.apache.aries.blueprint.core:1.6.2]
            at org.apache.aries.blueprint.container.BeanRecipe.runBeanProcInit(BeanRecipe.java:736)[33:org.apache.aries.blueprint.core:1.6.2]
            ... 34 more
    Caused by: org.apache.cxf.service.factory.ServiceConstructionException: No resource classes found
            at org.apache.cxf.jaxrs.AbstractJAXRSFactoryBean.checkResources(AbstractJAXRSFactoryBean.java:317)
            at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:159)
            ... 42 more
    

2 个答案:

答案 0 :(得分:0)

尝试将jax-rs注释放在服务接口上。

另一个可能的原因是未导入注释包。你阻止了你的pom中的大量进口。你可以检查没有这些定义。

答案 1 :(得分:0)

在我这边你的代码可以工作,但我不得不改变以下事项:

  1. 从蓝图中删除cxf:bus
  2. 删除实现类<{li>上的@Path
  3. 更改pom.xml
  4. 中的导入部分

    实现:

    package com.mycompany.testcxf;
    
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    
    public class MyRestServiceImpl implements MyRestService {
    
        @Override
        @GET
        @Path("/{echo}")
        @Produces(MediaType.APPLICATION_XML)
        public String pingMe(@PathParam("echo") String echo) {
            return "Knock Knock: " + echo + " !!";
        }
    }
    

    的pom.xml:

    <osgi.export.package>{local-packages}</osgi.export.package>
    <osgi.import.package>
        com.mycompany.testcxf*,
        *
    </osgi.import.package>
    

    我只导入依赖项:

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>2.7.13</version>
        </dependency>
    

    注意我无法像您一样测试CXF版本3,因为我的OSGI容器已绑定到此版本。但是这个设置对我来说非常合适,那么它可能是CXF版本的一个问题,并且与嵌入式http服务器(Jetty?)有些不兼容。

    我的设置为ServiceMix 5.1.4,其中包含:

    • Apache Karaf版本2.3.9
    • Camel版本2.13.3
    • CXF版本2.7.13