Использование Camel Apache cxfrs с SpringBoot - PullRequest
0 голосов
/ 05 июля 2019

Я работаю над созданием базового проекта, который использует camel cxfrs в Spring Boot. Базовые файлы, из которых я основываю свое приложение, взяты из ссылки на github: https://github.com/camelinaction/camelinaction2/tree/master/chapter10/camel-cxf-rest-spring-boot

Однако наше требование - использовать Undertow, поэтому я заменил реализацию на использование Undertow вместо Jetty (а также сервера Undertow вместо Tomcat - если эта информация имеет значение).

Вот мой pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>com.paulo.practice.application</groupId>
    <artifactId>sample-camel-cxf-spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sample-camel-cxf-spring-boot</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot-version>2.1.5.RELEASE</spring-boot-version>
        <camel-version>2.24.0</camel-version>
        <java.version>1.8</java.version>
        <cxf-version>3.2.7</cxf-version>
        <cxf-rt-rs-extension-providers.version>3.3.1</cxf-rt-rs-extension-providers.version>
        <!--<fabric8.version>3.0.11.fuse-731003-redhat-00004</fabric8.version>-->
        <fabric8.version>3.0.11.fuse-000065-redhat-3</fabric8.version>
    </properties>

    <!-- Using the dependencies from RedHat -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.fabric8</groupId>
                <artifactId>fabric8-project-bom-camel-spring-boot</artifactId>
                <version>${fabric8.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- Health check -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-boot-starter</artifactId>
        </dependency>

        <!-- test -->
        <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter-test</artifactId>-->
            <!--<scope>test</scope>-->
        <!--</dependency>-->
        <!--<dependency>-->
            <!--<groupId>org.apache.camel</groupId>-->
            <!--<artifactId>camel-test-spring</artifactId>-->
            <!--<scope>test</scope>-->
        <!--</dependency>-->

        <!-- Json Provider -->
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>org.apache.camel</groupId>-->
            <!--<artifactId>camel-cxf</artifactId>-->
        <!--</dependency>-->

        <!--<dependency>-->
            <!--<groupId>org.apache.cxf</groupId>-->
            <!--<artifactId>cxf-rt-ws-security</artifactId>-->
        <!--</dependency>-->

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jaxb-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-cxf-starter</artifactId>
        </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-rs-extension-providers</artifactId>
            <version>${cxf-version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-undertow</artifactId>
            <version>${cxf-version}</version>
        </dependency>

    </dependencies>

    <repositories>
        <repository>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>central</id>
            <name>Central Repository</name>
            <url>https://repo.maven.apache.org/maven2</url>
        </repository>
        <repository>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>never</updatePolicy>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>red-hat-ga-repository</id>
            <name>Red Hat GA Repository</name>
            <url>https://maven.repository.redhat.com/ga</url>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!--<version>${spring-boot-version}</version>-->
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Вот как объявляется маршрут:

@Component
public class OrderRoute extends RouteBuilder {

    @Bean(name = "jsonProvider")
    public JacksonJsonProvider jsonProvider() {
        return new JacksonJsonProvider();
    }

    @Override
    public void configure() throws Exception {
        // use CXF-RS to setup the REST web service using the resource class
        // and use the simple binding style which is recommended to use
        from("cxfrs:bean:cxfEndpoint?performInvocation=true&bindingStyle=SimpleConsumer&providers=#jsonProvider")
            .routeId("cxf-springboot-route")
            // call the route based on the operation invoked on the REST web service
            .toD("direct:${header.operationName}");

        // routes that implement the REST services
        from("direct:createOrder")
            .bean("orderService", "createOrder");

        from("direct:getOrder")
            .bean("orderService", "getOrder(${header.id})");

        from("direct:updateOrder")
            .bean("orderService", "updateOrder");

        from("direct:cancelOrder")
            .bean("orderService", "cancelOrder(${header.id})");
    }
}

У меня есть camel-context.xml для описания bean-компонента cxfrs.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:cxf="http://camel.apache.org/schema/cxf"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd        http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd">

    <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" id="logIn" />
    <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" id="logOut" />

    <cxf:rsServer
            address="http://localhost:8080" id="cxfEndpoint"
            serviceClass="com.paulo.practice.application.samplecamelcxfspringboot.RestOrderService">
        <cxf:providers>
            <ref bean="jsonProvider" />
        </cxf:providers>
        <cxf:inInterceptors>
            <ref bean="logIn" />
        </cxf:inInterceptors>
        <cxf:outInterceptors>
            <ref bean="logOut" />
        </cxf:outInterceptors>
    </cxf:rsServer>
</beans>

Я звоню в службу, вызывая:

curl -i localhost:8080/orders/1

и вот что я получаю в ответ

{"timestamp":1562301518768,"status":404,"error":"Not Found","message":"Not Found","path":"/orders/1"}

В логах вот что я получаю

2019-07-05 14:17:27.027 DEBUG 93257 --- [  XNIO-3 task-1] o.a.camel.spring.SpringCamelContext      : onApplicationEvent: ServletRequestHandledEvent: url=[/orders/1]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcherServlet]; session=[null]; user=[null]; time=[23ms]; status=[OK]
2019-07-05 14:17:27.133 DEBUG 93257 --- [  XNIO-3 task-1] o.a.camel.spring.SpringCamelContext      : onApplicationEvent: ServletRequestHandledEvent: url=[/error]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcherServlet]; session=[null]; user=[null]; time=[99ms]; status=[OK]

Он отвечает, но вызов сервера / orders / 1, кажется, не получен / не обработан сервером cxfrs? Похоже, что мне не хватает конфигурации или чего-то еще.

Я перемещался по разным баночкам и пружинным загрузчикам, но не могу исправить ошибку.

Код репо здесь https://github.com/paubengero/sample-camel-cxf-spring-boot

...