3

Trying to create a simple REST service and apply routing via the blueprint.xml file. I want to do this without Spring... - only blueprint

My attempts all end with the following exception:

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

Frustrated as I don't have enough "frame of reference" to detect whether there is a key component missing, or versioning issue, etc...

Appreciate any help identifying what is wrong/missing, etc.

Below is the simple app I am working with:

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>

Environment:

Java 8
apache-servicemix 7.0.1

FWIW - tried to derive this POC from existing example located at:

camel-cxf-rest

But, apparently, this example is 3+ years old... :-(

enter image description here

sairn
  • 461
  • 3
  • 24
  • 58

1 Answers1

1

You cannot use CXF with the rest-dsl, you need to use one of the supported components: http://camel.apache.org/rest-dsl

And then you need to install that from the ServiceMix shell via: feature:install camel-servlet etc.

See some of the REST examples from Apache Camel: https://github.com/apache/camel/tree/master/examples#examples

Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
  • Hi Claus - sorry so late getting back to this. I'm confused at your response because I see examples like this (https://github.com/apache/servicemix/tree/master/examples/camel/camel-cxf-rest) . I tried to model this simplistic POC (above) after it. What exactly is wrong? – sairn Jul 21 '17 at 13:58
  • You cannot use `` (aka rest-dsl) with Apache CXF or the camel-cxf component. That example you point to is 3 years old and do not use ``. If you do like that example without `` then you can do it like that. – Claus Ibsen Jul 21 '17 at 14:07
  • Ah, thx!... One of the issues I'm experiencing as I attempt to quickly grasp a practical knowledge of Servicemix/Camel/routing.... It's been a bit difficult for me to recognize/roundup examples that are current/complete and using "best practices". But, I'll get past this! :-) Let me try removing the "" stuff :-) – sairn Jul 21 '17 at 15:19
  • Look at the timestamp in github and you can see its 3 years old if you look at the java source code – Claus Ibsen Jul 21 '17 at 16:21
  • Yep - Thx, Claus. Now just to find an simpler REST example that fits what I am trying to accomplish using latest servicemix/camel versions. The example I derived the simple POC from seems to be a bit overcomplicated in any case. My POC cannot be a Spring "REST" application, but, should use JAX-RS, and if possible CDI, and preferably routing - initially - using just blueprint (XML). – sairn Jul 21 '17 at 18:11