使用CXF-RS组件时,为什么我们使用<cxf:rsServer>而不是普通的<jaxrs:server>?

Iva*_*ali 6 java spring web-services apache-camel cxfrs

作为这个问题的后续,我仍然对如何正确使用CXF-RS组件感到困惑.

我很困惑为什么我们需要<cxf:rsServer>用于指定CXF-RS端点的标签(或者甚至是这样的概念?),当我可以<jaxrs:server>完全使用标签时.

这是Camel和CXF的配置XML:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xmlns:camel="http://camel.apache.org/schema/spring"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd      
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
        http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

    <jaxrs:server id="userService" address="/users">
        <jaxrs:serviceBeans>
            <bean class="com.example.UserServiceNoop" />
        </jaxrs:serviceBeans>
        <jaxrs:providers>
            <bean class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider" />
        </jaxrs:providers>
    </jaxrs:server>

    <bean id="user" class="org.apache.camel.component.direct.DirectComponent" />

    <camel:camelContext id="someCamelContext">
        <camel:route id="userServiceRoute">
            <camel:from uri="cxfrs:bean:userService" />
            <camel:routingSlip>
                <camel:simple>user:${header.operationName}</camel:simple>
            </camel:routingSlip>
        </camel:route>

        <camel:route id="userServiceRetrieveUser">
            <from uri="user:retrieveUser" />
            <!-- Assume this is going to a useful Processor -->

        </camel:route>  
    </camel:camelContext>
</beans>
Run Code Online (Sandbox Code Playgroud)

UserService.java:

package com.example;

/* a bunch of imports... */

public interface UserService {
    @GET
    @Path(value="/{user.id}")
    @Produces({MediaType.APPLICATION_JSON})
    public User retrieveUser(
        @PathParam("user.id") Integer id
    );
}
Run Code Online (Sandbox Code Playgroud)

UserServiceNoop.java

package com.example;

/* a bunch of imports ... */

public class UserServiceNoop implements UserService 
{
    @Override
    public User retrieveUser(Integer id) {
        throw new RuntimeException();
    }
}
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我没有使用任何<cxf:rsServer>标签,但它工作正常.我知道它通过CXF-RS组件,因为当我运行应用程序时,它不会抛出任何RuntimeExceptions,这是使用CXF-RS时的预期行为(不会调用服务类中的方法实现).

我没有使用这个标签,我错过了什么?

谢谢!

小智 0

当您想要使用 CXF 端点作为某些内容的使用者时,您可以使用 cxf:reserver 标签。例如,在复杂的 Apache Camel 路由或 Spring 集成中。当您是端点服务请求的提供者时使用。