带有 OSGI 和 Karaf 的 CXF Web 服务 [英] CXF web services with OSGI and Karaf

查看:25
本文介绍了带有 OSGI 和 Karaf 的 CXF Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 OSGI 和 Karaf 创建一个简单的 Web 服务.

I am trying to create a simple web service with OSGI and Karaf.

以下是我进行的步骤:

  1. 下载 apache karaf - 3.0.3
  2. 启动karaf,执行以下命令
  3. 功能:repo-add cxf 3.0.
  4. 功能:安装 http cxf

然后创建了以下类:

界面

  public interface IMyService {
    String sayHello(
            String name);
    }

实施

@WebService
public class MyServiceImpl implements IMyService {

    @Override
    public String sayHello(
            final String name) {
        return "Hello " + name + " !!";
    }
}

OSGI 蓝图.xml

OSGI blueprint.xml

 <cxf:bus id="cxfBus1">
        <cxf:features>
            <cxf:logging />
        </cxf:features>
    </cxf:bus>
    <jaxrs:server address="/hello" id="helloService">
        <jaxrs:serviceBeans>
            <ref component-id="helloBean" />
        </jaxrs:serviceBeans>
    </jaxrs:server>
    <bean id="helloBean" class="org.test.ws.impl.MyServiceImpl" />

当 bundle 部署到 karaf deploy 目录时,bundle 无法安装.出现以下错误:

When the bundle is deployed into karaf deploy dir, the bundle cannot be installed. The following error occurs:

 2015-02-10 19:11:52,325 | ERROR | FelixStartLevel  | BlueprintContainerImpl           | 15 - org.apache.aries.blueprint.core - 1.4.2 | Unable to start blueprint container for bundle org.test.test-karaf-ws
org.osgi.service.blueprint.container.ComponentDefinitionException: Unable to initialize bean helloService
    at org.apache.aries.blueprint.container.BeanRecipe.runBeanProcInit(BeanRecipe.java:714)[15:org.apache.aries.blueprint.core:1.4.2]
    at org.apache.aries.blueprint.container.BeanRecipe.internalCreate2(BeanRecipe.java:824)[15:org.apache.aries.blueprint.core:1.4.2]
    at org.apache.aries.blueprint.container.BeanRecipe.internalCreate(BeanRecipe.java:787)[15:org.apache.aries.blueprint.core:1.4.2]
    at org.apache.aries.blueprint.di.AbstractRecipe$1.call(AbstractRecipe.java:79)[15:org.apache.aries.blueprint.core:1.4.2]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)[:1.8.0_25]
    at org.apache.aries.blueprint.di.AbstractRecipe.create(AbstractRecipe.java:88)[15:org.apache.aries.blueprint.core:1.4.2]
    at org.apache.aries.blueprint.container.BlueprintRepository.createInstances(BlueprintRepository.java:245)[15:org.apache.aries.blueprint.core:1.4.2]
    at org.apache.aries.blueprint.container.BlueprintRepository.createAll(BlueprintRepository.java:183)[15:org.apache.aries.blueprint.core:1.4.2]
    at org.apache.aries.blueprint.container.BlueprintContainerImpl.instantiateEagerComponents(BlueprintContainerImpl.java:682)[15:org.apache.aries.blueprint.core:1.4.2]
    at org.apache.aries.blueprint.container.BlueprintContainerImpl.doRun(BlueprintContainerImpl.java:377)[15:org.apache.aries.blueprint.core:1.4.2]
    at org.apache.aries.blueprint.container.BlueprintContainerImpl.run(BlueprintContainerImpl.java:269)[15:org.apache.aries.blueprint.core:1.4.2]
    at org.apache.aries.blueprint.container.BlueprintExtender.createContainer(BlueprintExtender.java:294)[15:org.apache.aries.blueprint.core:1.4.2]
    at org.apache.aries.blueprint.container.BlueprintExtender.createContainer(BlueprintExtender.java:263)[15:org.apache.aries.blueprint.core:1.4.2]
    at org.apache.aries.blueprint.container.BlueprintExtender.modifiedBundle(BlueprintExtender.java:253)[15:org.apache.aries.blueprint.core:1.4.2]
    at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.customizerModified(BundleHookBundleTracker.java:500)[9:org.apache.aries.util:1.1.0]
    at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.customizerModified(BundleHookBundleTracker.java:433)[9:org.apache.aries.util:1.1.0]
    at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$AbstractTracked.track(BundleHookBundleTracker.java:725)[9:org.apache.aries.util:1.1.0]
    at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$Tracked.bundleChanged(BundleHookBundleTracker.java:463)[9:org.apache.aries.util:1.1.0]
    at org.apache.aries.util.tracker.hook.BundleHookBundleTracker$BundleEventHook.event(BundleHookBundleTracker.java:422)[9:org.apache.aries.util:1.1.0]
    at org.apache.felix.framework.util.SecureAction.invokeBundleEventHook(SecureAction.java:1103)[org.apache.felix.framework-4.2.1.jar:]
    at org.apache.felix.framework.util.EventDispatcher.createWhitelistFromHooks(EventDispatcher.java:695)[org.apache.felix.framework-4.2.1.jar:]
    at org.apache.felix.framework.util.EventDispatcher.fireBundleEvent(EventDispatcher.java:483)[org.apache.felix.framework-4.2.1.jar:]
    at org.apache.felix.framework.Felix.fireBundleEvent(Felix.java:4403)[org.apache.felix.framework-4.2.1.jar:]
    at org.apache.felix.framework.Felix.startBundle(Felix.java:2092)[org.apache.felix.framework-4.2.1.jar:]
    at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1291)[org.apache.felix.framework-4.2.1.jar:]
    at org.apache.felix.framework.FrameworkStartLevelImpl.run(FrameworkStartLevelImpl.java:304)[org.apache.felix.framework-4.2.1.jar:]
    at java.lang.Thread.run(Thread.java:745)[:1.8.0_25]
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_25]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)[:1.8.0_25]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.8.0_25]
    at java.lang.reflect.Method.invoke(Method.java:483)[:1.8.0_25]
    at org.apache.aries.blueprint.utils.ReflectionUtils.invoke(ReflectionUtils.java:297)[15:org.apache.aries.blueprint.core:1.4.2]
    at org.apache.aries.blueprint.container.BeanRecipe.invoke(BeanRecipe.java:958)[15:org.apache.aries.blueprint.core:1.4.2]
    at org.apache.aries.blueprint.container.BeanRecipe.runBeanProcInit(BeanRecipe.java:712)[15:org.apache.aries.blueprint.core:1.4.2]
    ... 26 more
Caused by: org.apache.cxf.service.factory.ServiceConstructionException: No resource classes found
    at org.apache.cxf.jaxrs.AbstractJAXRSFactoryBean.checkResources(AbstractJAXRSFactoryBean.java:318)
    at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:159)
    ... 34 more

我在这里遗漏了什么?

推荐答案

对于 RESTful Web 服务,您需要这样的蓝图:

For RESTful Web Service you need a blueprint like this:

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.1.0"
           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"
           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
  http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.1.0 http://aries.apache.org/schemas/blueprint-ext/blueprint-ext-1.1.xsd
  ">
    <cxf:bus id="myServiceBus">
    </cxf:bus>

    <bean id="myServiceImpl" class="org.test.ws.impl.MyServiceImpl"/>

    <jaxrs:server address="/hello" id="helloService">
        <jaxrs:serviceBeans>
            <ref component-id="myServiceImpl" />
        </jaxrs:serviceBeans>
        <jaxrs:providers>
        </jaxrs:providers>
    </jaxrs:server>

</blueprint>

然后你需要移除@WebService注解并使用javax.ws.rs.*注解来定义你想要为每个发布的RESTful服务使用的HTTP方法喜欢:

Then you need to remove @WebService annotation and use the javax.ws.rs.* annotations to define the HTTP method you want to use for each RESTful service published like:

public class MyServiceImpl implements IMyService {

    @GET
    @Path("/{name}")
    @Produces(MediaType.APPLICATION_XML)
    public String sayHello(@PathParam("name") final String name) {
        return "Hello " + name + " !!";
    }
}

通过此配置,您可以调用您的 RESTful 服务:http://hostname:8181/cxf/hello/我的名字

With this configuration you can invoke your RESTful service as: http://hostname:8181/cxf/hello/MYNAME

希望这对现在有所帮助...

Hope this helps now...

顺便说一下,我的 pom.xml 上有这些 deps:

By the way I have these deps on my pom.xml:

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

这篇关于带有 OSGI 和 Karaf 的 CXF Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆