且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

带有 OSGI 和 Karaf 的 CXF Web 服务

更新时间:2023-11-18 17:26:16

对于 RESTful Web 服务,您需要这样的蓝图:

For RESTful Web Service you need a blueprint like this:

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.1.0"
           xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
           xmlns:cxf="http://cxf.apache.org/blueprint/core"
           xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs"
           xsi:schemaLocation="
  http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
  http://www.osgi.org/xmlns/blueprint-ext/v1.1.0 https://svn.apache.org/repos/asf/aries/tags/blueprint-0.3.1/blueprint-core/src/main/resources/org/apache/aries/blueprint/ext/blueprint-ext.xsd
  http://cxf.apache.org/blueprint/jaxws http://cxf.apache.org/schemas/blueprint/jaxws.xsd
  http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd
  http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
  http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd
  http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd
  http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.1.0 http://aries.apache.org/schemas/blueprint-ext/blueprint-ext-1.1.xsd
  ">
    <cxf:bus id="myServiceBus">
    </cxf:bus>

    <bean id="myServiceImpl" class="org.test.ws.impl.MyServiceImpl"/>

    <jaxrs:server address="/hello" id="helloService">
        <jaxrs:serviceBeans>
            <ref component-id="myServiceImpl" />
        </jaxrs:serviceBeans>
        <jaxrs:providers>
        </jaxrs:providers>
    </jaxrs:server>

</blueprint>

然后你需要移除@WebService注解并使用javax.ws.rs.*注解来定义你想要为每个发布的RESTful服务使用的HTTP方法喜欢:

Then you need to remove @WebService annotation and use the javax.ws.rs.* annotations to define the HTTP method you want to use for each RESTful service published like:

public class MyServiceImpl implements IMyService {

    @GET
    @Path("/{name}")
    @Produces(MediaType.APPLICATION_XML)
    public String sayHello(@PathParam("name") final String name) {
        return "Hello " + name + " !!";
    }
}

通过此配置,您可以调用您的 RESTful 服务:http://hostname:8181/cxf/hello/我的名字

With this configuration you can invoke your RESTful service as: http://hostname:8181/cxf/hello/MYNAME

希望这对现在有所帮助...

Hope this helps now...

顺便说一下,我的 pom.xml 上有这些 deps:

By the way I have these deps on my pom.xml:

    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>jsr311-api</artifactId>
        <version>1.1.1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxrs</artifactId>
        <version>${cxf.version}</version>
    </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-transports-http-jetty</artifactId>
        <version>${cxf.version}</version>
    </dependency>