eclipse 如何创建 Restful 服务并部署它的 OSGi 容器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15797501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create Restful service and deploy it OSGi container?
提问by Li'
My goal is to create a Restful service Maven project with Eclipse. Then package it as a bundle and deploy it to Fuse ESB karaf OSGi container. So far what I know is how to use the JAX-RS API annotations, @Path @GET:
我的目标是使用 Eclipse 创建一个 Restful 服务 Maven 项目。然后将其打包为一个包并将其部署到 Fuse ESB karaf OSGi 容器中。到目前为止,我所知道的是如何使用 JAX-RS API 注释,@Path @GET:
package com.restfultest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("/example")
public class ExampleService {
@GET
public String sayHello() {
return "Hello Restful service";
}
}
My question is that: 1. what maven archetype should I use? maven-archetype-webapp or quickstart?
我的问题是: 1. 我应该使用什么 maven 原型?maven-archetype-webapp 还是快速入门?
2.How to implement Activator? Like this?
2.如何实现Activator?像这样?
public class Activator implements BundleActivator {
private ServiceRegistration<?> registration;
public void start(BundleContext context) throws Exception {
// TODO Auto-generated method stub
ExampleService exampleService = new ExampleService();
registration = context.registerService( ExampleService.class.getName(), exampleService, null );
}
public void stop(BundleContext context) throws Exception {
// TODO Auto-generated method stub
registration.unregister();
}
}
3. How to register and publish the service (like how to configure the Endpoint address and port)?
3.如何注册和发布服务(比如如何配置Endpoint地址和端口)?
I am new to osgi. Does anyone can provide me some resources or a detailed tutorial?
我是 osgi 的新手。有没有人可以给我一些资源或详细的教程?
回答by Dmytro Pishchukhin
- You can use Maven bundle plugin
- and 3. Apache CXF DOSGihelps you to publish OSGi services as WS/REST.
- 您可以使用Maven 捆绑插件
- 和 3. Apache CXF DOSGi帮助您将 OSGi 服务发布为 WS/REST。
回答by Marco Vargas
Here is my 5 cent:
这是我的 5 美分:
1. What Maven archetype should you use?
1. 你应该使用什么 Maven 原型?
- I used the OSGI HTTP Service (Quick Start), check thissample, I considered it was more natural.
- 我使用了 OSGI HTTP Service (Quick Start),查看这个示例,我认为它更自然。
2. How to implement Activator?
2. 如何实现激活器?
- Go ahead and use the Activatorof the sample, it worked for me.
- If you use it, you also will need to implement the the Applicationand the Rest Service
3. How to register and publish the service?
3.如何注册和发布服务?
- I recommend you to download the latest jersey-ALL-bundleand install it on your OSGI.
And then, in my particular case, I used the Maven Bundle Pluginto handle the imports on Runtime and packing, so, my pom.xml looks something like this (Please notice the dependencies):
... <packaging>bundle</packaging> <build> <plugins> <!--+ + OSGi Bundle-Manifiest Generator. + --> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>2.3.7</version> <extensions>true</extensions> <configuration> <instructions> <Import-Package>javax.servlet.*;version="[2.4,4.0)",*</Import-Package> <Bundle-Activator>com.sample.api.Activator</Bundle-Activator> <Implementation-Title>jersey-osgi-http-service-bundle</Implementation-Title> <Implementation-Version>${project.version}</Implementation-Version> </instructions> <unpackBundle>true</unpackBundle> </configuration> </plugin> </plugins> </build> <dependencies> <!--+======================+--> <!--+ REST Dependencies +--> <!--+======================+--> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> <version>2.22.1</version> </dependency> <dependency> <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.http.bundle</artifactId> <version>2.2.0</version> </dependency> <!--+=========================================+--> <!--+ Apache Felix Framework (OSGi framework) +--> <!--+=========================================+--> <dependency> <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.framework</artifactId> <version>4.6.0</version> </dependency> </dependencies>
In my specific case I have also the WebConsole installed, then my bundles look like this:
0|Active | 0|System Bundle (4.6.0) 1|Active | 1|Apache Felix Bundle Repository (2.0.2) 2|Active | 1|Apache Felix Gogo Command (0.14.0) 3|Active | 1|Apache Felix Gogo Runtime (0.12.1) 4|Active | 1|Apache Felix Gogo Shell (0.10.0) 5|Active | 1|com.sample-api (1.3.0.SNAPSHOT) 6|Active | 1|jersey-all (2.22.1) 7|Active | 1|Apache Felix Log Service (1.0.0) 8|Active | 1|Apache Felix Configuration Admin Service (1.2.4) 9|Active | 1|Apache Felix Shell Service (1.4.2) 10|Active | 1|Apache Felix Http Bundle (2.0.4) 11|Active | 1|HTTP Service (1.0.0) 12|Active | 1|Apache Felix Web Management Console (3.1.2)
For Rest it's important to have the bundles 6, 10 and 11.
And the port is configurable in the "config.properties" of the OSGI (I used Felix) just by doing this:
org.osgi.service.http.port=28370
Then, to reach the service just use the following path:
http://localhost:28370/jersey-http-service/status
- 我建议您下载最新的jersey-ALL-bundle并将其安装在您的 OSGI 上。
然后,在我的特定情况下,我使用Maven Bundle Plugin来处理运行时和打包的导入,因此,我的 pom.xml 看起来像这样(请注意依赖项):
... <packaging>bundle</packaging> <build> <plugins> <!--+ + OSGi Bundle-Manifiest Generator. + --> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>2.3.7</version> <extensions>true</extensions> <configuration> <instructions> <Import-Package>javax.servlet.*;version="[2.4,4.0)",*</Import-Package> <Bundle-Activator>com.sample.api.Activator</Bundle-Activator> <Implementation-Title>jersey-osgi-http-service-bundle</Implementation-Title> <Implementation-Version>${project.version}</Implementation-Version> </instructions> <unpackBundle>true</unpackBundle> </configuration> </plugin> </plugins> </build> <dependencies> <!--+======================+--> <!--+ REST Dependencies +--> <!--+======================+--> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> <version>2.22.1</version> </dependency> <dependency> <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.http.bundle</artifactId> <version>2.2.0</version> </dependency> <!--+=========================================+--> <!--+ Apache Felix Framework (OSGi framework) +--> <!--+=========================================+--> <dependency> <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.framework</artifactId> <version>4.6.0</version> </dependency> </dependencies>
在我的特定情况下,我还安装了WebConsole,然后我的包看起来像这样:
0|Active | 0|System Bundle (4.6.0) 1|Active | 1|Apache Felix Bundle Repository (2.0.2) 2|Active | 1|Apache Felix Gogo Command (0.14.0) 3|Active | 1|Apache Felix Gogo Runtime (0.12.1) 4|Active | 1|Apache Felix Gogo Shell (0.10.0) 5|Active | 1|com.sample-api (1.3.0.SNAPSHOT) 6|Active | 1|jersey-all (2.22.1) 7|Active | 1|Apache Felix Log Service (1.0.0) 8|Active | 1|Apache Felix Configuration Admin Service (1.2.4) 9|Active | 1|Apache Felix Shell Service (1.4.2) 10|Active | 1|Apache Felix Http Bundle (2.0.4) 11|Active | 1|HTTP Service (1.0.0) 12|Active | 1|Apache Felix Web Management Console (3.1.2)
对于 Rest,拥有 6、10 和 11 包很重要。
只需执行以下操作,即可在 OSGI(我使用 Felix)的“config.properties”中配置端口:
org.osgi.service.http.port=28370
然后,要访问该服务,只需使用以下路径:
http://localhost:28370/jersey-http-service/status
回答by ilikeorangutans
You'll need to create an OSGI bundle. There are a few archetypes that create maven projects:
Your activator looks correct.
This question I cannot answer, but it looks like this project does what you need: https://github.com/hstaudacher/osgi-jax-rs-connector
您需要创建一个 OSGI 包。有一些原型可以创建 maven 项目:
您的激活器看起来正确。
这个问题我无法回答,但看起来这个项目可以满足您的需求:https: //github.com/hstaudacher/osgi-jax-rs-connector
In general I recommend you get yourself the OSGI specification, it's a good read and explains a lot of things.
一般来说,我建议您自己阅读OSGI 规范,这是一本很好的读物并解释了很多事情。