在SpringBoot中使用Camel Apache cxfrs

时间:2019-07-05 05:26:45

标签: spring-boot apache-camel cxfrs

我正在创建一个在Spring Boot中使用骆驼cxfrs的基础项目。我基于应用程序的基础文件来自以下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来描述cxfrs bean。

<?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]

它正在响应,但是cxfrs服务器似乎未收到/未处理对/ orders / 1的调用?似乎是我缺少配置或某些东西。

我在不同的罐子和弹簧靴启动器上四处走动,但我似乎无法纠正该错误。

代码仓库位于https://github.com/paubengero/sample-camel-cxf-spring-boot

0 个答案:

没有答案