2017-07-18 58 views
2

嘗試創建簡單的REST服務並通過blueprint.xml文件應用路由。我想這樣做沒有春天? - 只有藍圖無法創建簡單的REST服務並通過blueprint.xml文件應用路由。我想在沒有Spring的情況下執行此操作... - 僅藍圖

我嘗試全部結束與以下異常:

"java.lang.IllegalStateException: Cannot find RestConsumerFactory in Registry or as a Component to use" 

沮喪,因爲我沒有足夠的「參照系」,以檢測是否有是一個關鍵部件丟失,或版本問題,等等

感謝所有幫助確定什麼是錯誤/丟失等

下面是簡單的應用程序,我有工作:

project structure

RestPostService.java

package aaa.bbb.ccc; 

import javax.ws.rs.Path; 
import javax.ws.rs.GET; 
import javax.ws.rs.POST; 
import javax.ws.rs.core.Response; 
import javax.ws.rs.Produces; 
import javax.ws.rs.Consumes; 
import javax.ws.rs.core.MediaType; 
import org.apache.logging.log4j.LogManager; 
import org.apache.logging.log4j.Logger; 

@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) 
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) 
@Path("/service") 
public class RestPostService { 

    private static final Logger LOG = LogManager.getLogger("WebServiceSB"); 
    public RestPostService() { 
    } 

    @Path("/get1")  
    @GET 
    public ThePojo get1() { 
    ThePojo tp = new ThePojo("aaa", "bbb"); 
    return tp; 
    }  

    @Path("/post1")   
    @POST 
    @Produces(MediaType.TEXT_HTML)  
    public Response post1(ThePojo tp) { 
    return Response.status(200).build(); 
    } 
} 

ThePojo.java

package aaa.bbb.ccc; 

public class ThePojo { 

    public ThePojo() { 
    }  

    private String field1; 
    private String field2; 

    public String getField1() { 
    return field1; 
    } 
    public void setField1(String field1) { 
    this.field1 = field1; 
    } 

    public String getField2() { 
    return field2; 
    } 
    public void setField2(String field2) { 
    this.field2 = field2; 
    } 

    public ThePojo(String field1, String field2) { 
    this.field1 = field1; 
    this.field2 = field2; 
    } 

    @Override 
    public String toString() { 
    return "ThePojo{" + "field1=" + field1 + ", field2=" + field2 + '}'; 
    } 
} 

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:cxf="http://camel.apache.org/schema/blueprint/cxf" 
     xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0" 
     xsi:schemaLocation=" 
     http://www.osgi.org/xmlns/blueprint/v1.0.0 
     http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd 
     http://camel.apache.org/schema/blueprint 
     http://camel.apache.org/schema/blueprint/camel-blueprint.xsd"> 
    <cxf:rsServer id="rsServer" address="http://localhost:8989/restPostService" 
      serviceClass="aaa.bbb.ccc.RestPostService" 
      loggingFeatureEnabled="true" loggingSizeLimit="20"/>  

    <camelContext xmlns="http://camel.apache.org/schema/blueprint"> 
    <rest> 
     <get uri="/service/get1"> 
     <to uri="direct:get1"/> 
     </get> 
     <post uri="/service/post1" consumes="application/json"> 
     <to uri="direct:post1"/> 
     </post> 
    </rest> 
    <route> 
     <from uri="direct:post1"/> 
     <to uri="activemq:queue:queueA"/> 
    </route> 
    </camelContext> 
</blueprint> 

的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/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>aaa.bbb.ccc</groupId> 
    <artifactId>restPost</artifactId> 
    <version>1</version> 
    <packaging>jar</packaging> 

    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <maven.compiler.source>1.8</maven.compiler.source> 
    <maven.compiler.target>1.8</maven.compiler.target> 
    </properties> 

    <dependencies> 
    <dependency> 
     <groupId>org.apache.cxf</groupId> 
     <artifactId>cxf-rt-frontend-jaxrs</artifactId> 
     <version>3.1.11</version> 
     <type>jar</type> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.cxf</groupId> 
     <artifactId>cxf-rt-transports-http</artifactId> 
     <version>3.1.11</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.cxf</groupId> 
     <artifactId>cxf-bundle-jaxrs</artifactId> 
     <version>2.7.18</version> 
    </dependency>   
    <dependency> 
     <groupId>org.apache.logging.log4j</groupId> 
     <artifactId>log4j-api</artifactId> 
     <version>2.6.2</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.logging.log4j</groupId> 
     <artifactId>log4j-core</artifactId> 
     <version>2.6.2</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.camel</groupId> 
     <artifactId>camel-cxf</artifactId> 
     <version>2.19.1</version> 
    </dependency>    
    <dependency> 
     <groupId>com.fasterxml.jackson.jaxrs</groupId> 
     <artifactId>jackson-jaxrs-json-provider</artifactId> 
     <version>2.8.9</version> 
     <type>jar</type> 
    </dependency> 
    <dependency> 
     <groupId>javax</groupId> 
     <artifactId>javaee-api</artifactId> 
     <version>7.0</version> 
     <type>jar</type> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.aries.blueprint</groupId> 
     <artifactId>org.apache.aries.blueprint</artifactId> 
     <version>1.1.0</version> 
    </dependency> 
    </dependencies> 

    <build> 
    <!-- use for snapshot versioning <finalName>${project.artifactId}-${project.version}-${maven.build.timestamp}</finalName> --> 
    <finalName>${project.artifactId}-${project.version}</finalName> 
    <plugins> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>3.1</version> 
     <configuration> 
      <source>1.8</source> 
      <target>1.8</target> 
      <showDeprecation>true</showDeprecation> 
     </configuration> 
     </plugin>  
    </plugins> 
    </build>  
</project> 

環境:

Java 8 
apache-servicemix 7.0.1 

FWIW - 試圖獲得從現有例如這個POC位於:

camel-cxf-rest

但是,很顯然,這個例子是3+歲...: - (

enter image description here

回答

0

你不能休息,DSL使用CXF,需要使用支持的組件之一:http://camel.apache.org/rest-dsl

然後你需要通過安裝從ServiceMix的外殼:feature:install camel-servlet

看到一些來自阿帕奇駱駝的REST例子:https://github.com/apache/camel/tree/master/examples#examples

+0

嗨克勞斯 - 對不起這麼晚回到這個。我對你的迴應感到困惑,因爲我看到這樣的例子(https://github.com/apache/servicemix/tree/master/examples/camel/camel-cxf-rest)。之後我試圖對這個簡單的POC(上圖)進行建模。究竟是什麼錯誤? – sairn

+0

您不能在Apache CXF或camel-cxf組件中使用''(又名rest-dsl)。你指出的那個例子是3歲,並且不使用''。如果你喜歡那個沒有''的例子,那麼你可以這樣做。 –

+0

啊,thx!...我試圖快速掌握Servicemix/Camel/routing的實際知識時遇到的一個問題....對於我來說,識別/彙總最新的示例有點困難/完成並使用「最佳實踐」。但是,我會通過這個!:-)讓我試着刪除「」的東西:-) – sairn

相關問題