kara容器搭建: https://blog.csdn.net/zhaosongbin/article/details/88393574

karaf+osgi+maven基础框架搭建: https://blog.csdn.net/zhaosongbin/article/details/88570841

karaf+osgi+servlet 本文项目下载地址: https://download.csdn.net/download/zhaosongbin/11022245


osgi实现servlet主要是servlet集成osgi的feature实现的,相关的依赖主要是
	maven:
		javax.ws.rs-api
		
	osgi或者是karaf:
		http-whiteboard
    	aries-jax-rs-whiteboard

首先在先前的基础框架上边新建maven项目 karaf-osgi-servlet

pom.xml

注意打包方式,osgi项目的打包方式不是我们常用的war或者jar,而是bundle

<packaging>bundle</packaging>

因为打包方式是bundle,所以必须要加上maven-bundle-plugin插件

<?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">
    <parent>
        <artifactId>karaf-osgi</artifactId>
        <groupId>com.karaf.osgi</groupId>
        <version>1.0.0</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.karaf.osgi</groupId>
    <artifactId>karaf-osgi-servlet</artifactId>

    <!--打包方式必须是bundle,有可能在pom中bundle打包方式会报错,
    此时在下方插件maven-bundle-plugin中添加<extensions>true</extensions>-->
    <packaging>bundle</packaging>
    <name>Karaf :: Osgi :: Servlet</name>

    <dependencies>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>osgi.cmpn</artifactId>
            <version>6.0.0</version>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.1</version>
        </dependency>
    </dependencies>

    <build>
        <!--feature.xml 所需,可以提出来单独作为一个服务,如果提取出来,在此pom中删除-开始-->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <targetPath>${project.build.directory}/resources</targetPath>
            </resource>
        </resources>
        <!--feature.xml 所需,可以提出来单独作为一个服务,如果提取出来,在此pom中删除-结束-->

        <plugins>

            <!--feature.xml 所需,可以提出来单独作为一个服务,如果提取出来,在此pom中删除-开始-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>resources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-artifacts</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attach-artifact</goal>
                        </goals>
                        <configuration>
                            <artifacts>
                                <artifact>
                                    <file>target/resources/feature.xml</file>
                                    <type>xml</type>
                                </artifact>
                            </artifacts>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!--feature.xml 所需,可以提出来单独作为一个服务,如果提取出来,在此pom中删除-结束-->



            <plugin>
                <!--bundle打包方式需要用的插件,为了实现osgi的服务-->
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Import-Package>
                            *
                        </Import-Package>
                        <Private-Package>
                            <!--项目中类所在的根目录-->
                            com.karaf.osgi.servlet
                        </Private-Package>
                    </instructions>
                </configuration>
            </plugin>
            
        </plugins>
    </build>
</project>

TestServlet.java

在java目录下新建根目录文件夹: com.karaf.osgi.servlet

在根目录下新建java文件:TestServlet.java
@path("")中就是我们需要访问的路径

package com.karaf.osgi.servlet;

import org.osgi.service.component.annotations.Component;

import javax.ws.rs.*;

@Path("/test")
@Component(service = TestServlet.class, property = {"osgi.jaxrs.resource=true"})
public class TestServlet {


    @Path("/hello")
    @Produces("application/json")
    @GET
    public String hello() {
        return "hello";
    }

    @Path("/test")
    @Consumes("application/json")
    @POST
    public String test() {
        return "test";
    }
}


feature.xml

在resource目录下新建文件:feature.xml

osgi服务运行需要的依赖

<feature>http-whiteboard</feature>
<feature>aries-jax-rs-whiteboard</feature>
<?xml version="1.0" encoding="UTF-8"?>

<features name="karaf-osgi-servlet-feature-${project.version}"
          xmlns="http://karaf.apache.org/xmlns/features/v1.4.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://karaf.apache.org/xmlns/features/v1.4.0">

    <feature name="karaf-osgi-servlet" version="${project.version}">
        <feature>http-whiteboard</feature>
        <feature>aries-jax-rs-whiteboard</feature>

        <bundle>mvn:${project.groupId}/karaf-osgi-servlet/${project.version}</bundle>
    </feature>

</features>


karaf 部署

karaf@root()> feature:repo-add mvn:com.karaf.osgi/karaf-osgi-servlet/1.0.0/xml
Adding feature url mvn:com.karaf.osgi/karaf-osgi-servlet/1.0.0/xml

karaf@root()> feature:install karaf-osgi-servlet
karaf@root()> http:list

ID  │ Servlet      │ Servlet-Name │ State       │ Alias │ Url
────┼──────────────┼──────────────┼─────────────┼───────┼─────
226 │ Whiteboard$2 │ cxf-servlet  │ Deployed    │       │ [/*]

karaf@root()> list
START LEVEL 100 , List Threshold: 50
 ID │ State  │ Lvl │ Version            │ Name
────┼────────┼─────┼────────────────────┼───────────────────────────────────────────────
 23 │ Active │  80 │ 4.2.3              │ Apache Karaf :: OSGi Services :: Event
220 │ Active │  80 │ 1.0.0              │ Karaf :: Osgi :: Servlet
225 │ Active │  80 │ 1.0.1              │ Apache Aries JAX-RS Specification API
226 │ Active │  80 │ 1.0.3              │ Apache Aries JAX-RS Whiteboard
234 │ Active │  80 │ 1.3.0.1            │ Apache ServiceMix :: Specs :: Annotation API 1.3
235 │ Active │  80 │ 4.12.0             │ Apache XBean OSGI Bundle Utilities
236 │ Active │  80 │ 4.12.0             │ Apache XBean :: Classpath Resource Finder
264 │ Active │  80 │ 7.0.0              │ org.objectweb.asm
265 │ Active │  80 │ 7.0.0              │ org.objectweb.asm.commons
266 │ Active │  80 │ 7.0.0              │ org.objectweb.asm.tree
273 │ Active │  80 │ 1.0.0.201802012106 │ org.osgi:org.osgi.service.jaxrs
274 │ Active │  80 │ 1.1.0.201802012106 │ org.osgi:org.osgi.util.function
275 │ Active │  80 │ 1.1.0.201802012106 │ org.osgi:org.osgi.util.promise
karaf@root()>

浏览器访问

在这里插入图片描述

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐