2015-02-10 113 views
2

我想創建一個簡單的Web服務與OSGI和卡拉夫。CXF Web服務與OSGI和卡拉夫

下面是我進行的步驟:

  1. 下載Apache karaf - 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 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" /> 

將軟件包部署到karaf部署目錄中時,無法安裝該軟件包。發生以下錯誤:

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 

我在這裏錯過了什麼?

回答

-1

對於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> 
+0

不,我不想JAX-WS。我需要RESTful Web服務(JAX-RS)。 – grozandrei 2015-06-02 17:42:13

+0

對不起,這個問題不是很清楚......「一個簡單的Web服務」...... – 2015-06-02 18:52:21

+0

@JorgeMartinez你有沒有使用JAX-WS公開OSGi實現的例子? – Twimnox 2016-11-17 15:32:49

1

對於RESTful Web服務,你需要這樣的藍圖:

<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這些DEPS方式:

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

你需要兩個變化:

1-更改您的IMPL爲:

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-前端-JAXRS依賴於你的pom.xml文件:

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