Веб-сервисы CXF с OSGI и Karaf

Я пытаюсь создать простой веб-сервис с OSGI и Karaf.

Вот шаги, которые я проделал:

  1. скачать апач караф - 3.0.3
  2. запустите karaf, выполните следующие команды
  3. особенность: репо-добавить 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

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

Когда пакет развернут в каталоге karaf deploy, пакет не может быть установлен. Возникает следующая ошибка:

 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

Что мне здесь не хватает?


person grozandrei    schedule 10.02.2015    source источник


Ответы (3)


Для веб-службы RESTful вам понадобится такой план:

<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.*, чтобы определить метод HTTP, который вы хотите использовать для каждой опубликованной службы RESTful, например:

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/MYNAME< /а>

Надеюсь, теперь это поможет...

Кстати, в моем 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>
person Jorge Martinez    schedule 02.06.2015

Вам нужно два изменения:

1- Измените свой импл как:

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

@Path("/myService")
public class MyServiceImpl implements IMyService {

    @GET
    @Path("/sayHello")
    @Produces(MediaType.APPLICATION_XML)

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

2- добавьте зависимость cxf-rt-frontend-jaxrs в ваш файл pom.xml:

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxrs</artifactId>
        <version>3.0.2</version>
    </dependency>
person Abolfazl Hashemi    schedule 24.05.2017

Для WebService вам нужно использовать jaxws.

Попробуйте с этим планом:

<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: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://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"/>

    <jaxws:endpoint
            implementor="#myServiceImpl"
            address="/hello"
            implementorClass="org.test.ws.impl.MyServiceImpl"/>

</blueprint>
person Jorge Martinez    schedule 02.06.2015
comment
Нет, я не хочу JAX-WS. Мне нужны веб-сервисы RESTful (JAX-RS). - person grozandrei; 02.06.2015
comment
@JorgeMartinez, у вас есть пример использования JAX-WS для демонстрации реализаций OSGi? - person Twimnox; 17.11.2016