凯发k8天生赢家一触即发

原创:equinox osgi应用嵌入jersey框架搭建rest服务 -凯发k8天生赢家一触即发

2023-10-20

一、环境

eclipse版本:eclipse-luna 4.4

jre版本:1.8

二、equinox osgi应用嵌入jersey框架搭建rest服务

1.新建插件工程hellowebosgi

a.

b.

c.

d.在新建的工程中新建文件夹lib,其中放入附件中的jar包(见文末),全部添加到工程build path中。

2.配置运行环境

a.配置引入包、依赖插件

b.选择run->run configuration,new一个环境

保留图中targetplatform中的16个bundle。

c.在run configuration中设置参数

友情提示:如果有其他异常请查看提示信息,利用 http://www.aolsearch.com/ 查询提示信息(英文),如果发现http://stackoverflow.com/这个网站有相同提问,基本就知道答案近在咫尺了。

比如笔者遇到的问题:root exception:java.lang.illegalstateexception: workbench has not been created yet.

解决方法就是在http://stackoverflow.com/questions/13773582/workbench-has-not-been-created-yet-error-in-eclipse-plugin-programming中找到的。

d.在manifest.mf(meta-inf)文件中将lib文件夹中的包都引入如下图中。

3.添加代码

activator_sample.java

 package helloworldosgi;
 import java.util.dictionary;
import java.util.hashmap;
import java.util.hashtable;
import java.util.logging.logger; import javax.servlet.servletexception; import org.glassfish.jersey.servlet.servletcontainer;
import org.osgi.framework.bundleactivator;
import org.osgi.framework.bundlecontext;
import org.osgi.framework.servicereference;
import org.osgi.service.event.event;
import org.osgi.service.event.eventadmin;
import org.osgi.service.http.httpservice;
import org.osgi.service.http.namespaceexception;
import org.osgi.util.tracker.servicetracker; /*
* bundleactivator:让你能够捕捉到bundle的start和stop事件,并对这两个事件作出自定义的反应。
*/
public class activator_sample implements bundleactivator {
private bundlecontext bc;
@suppresswarnings("rawtypes")
private servicetracker tracker;
private httpservice httpservice = null;
private static final logger logger = logger.getlogger(activator.class.getname()); /**
* url前缀
*/
public static final string context_path = "/rest/json"; /*
* bundlecontext:一个bundle在框架中的执行时上下文,这个上下文提供了和框架进行交互的方法。
* @see org.osgi.framework.bundleactivator#start(org.osgi.framework.bundlecontext)
*/
@suppresswarnings({ "unchecked", "rawtypes" })
@override
public synchronized void start(bundlecontext bundlecontext) throws exception {
this.bc = bundlecontext;
logger.info("starting http service bundle"); this.tracker = new servicetracker(this.bc, httpservice.class.getname(), null) {
@override
public object addingservice(servicereference serviceref) {
httpservice = (httpservice) super.addingservice(serviceref);
registerservlets();
return httpservice;
} @override
public void removedservice(servicereference ref, object service) {
if (httpservice == service) {
unregisterservlets();
httpservice = null;
}
super.removedservice(ref, service);
}
}; this.tracker.open(); logger.info("http service bundle started");
} /*
* bundlecontext:一个bundle在框架中的执行时上下文,这个上下文提供了和框架进行交互的方法。
* @see org.osgi.framework.bundleactivator#stop(org.osgi.framework.bundlecontext)
*/
@override
public synchronized void stop(bundlecontext bundlecontext) throws exception {
this.tracker.close();
} private void registerservlets() {
try {
rawregisterservlets();
} catch (interruptedexception | namespaceexception | servletexception ie) {
throw new runtimeexception(ie);
}
} private void rawregisterservlets() throws servletexception, namespaceexception, interruptedexception {
logger.info("jersey bundle: registering servlets");
logger.info("jersey bundle: http service = " httpservice.tostring()); // todo - temporary workaround
// this is a workaround related to issue jersey-2093; grizzly (1.9.5)
// needs to have the correct context
// classloader set
classloader myclassloader = getclass().getclassloader();
classloader originalcontextclassloader = thread.currentthread()
.getcontextclassloader();
try {
thread.currentthread().setcontextclassloader(myclassloader);
httpservice.registerservlet(context_path, new servletcontainer(),
getjerseyservletparams(), null);
httpservice.registerresources(context_path "/hello", "/webroot", null);//前面必须带“/”,后面不一定
} finally {
thread.currentthread().setcontextclassloader(
originalcontextclassloader);
}
// end of workaround - after grizzly updated to the recent version, only
// the inner call from try block will remain:
// httpservice.registerservlet("/jersey-http-service", new
// servletcontainer(), getjerseyservletparams(), null); sendadminevent();
logger.info("jersey bundle: servlets registered");
} @suppresswarnings("serial")
private void sendadminevent() {
@suppresswarnings("rawtypes")
servicereference earef = bc.getservicereference(eventadmin.class
.getname());
if (earef != null) {
@suppresswarnings("unchecked")
eventadmin ea = (eventadmin) bc.getservice(earef);
ea.sendevent(new event("jersey/test/deployed",
new hashmap() {
{
put("context-path", "/");
}
}));
bc.ungetservice(earef);
}
} private void unregisterservlets() {
if (this.httpservice != null) {
logger.info("jersey bundle: unregistering servlets");
httpservice.unregister(context_path);
logger.info("jersey bundle: servlets unregistered");
}
} private dictionary getjerseyservletparams() {
dictionary jerseyservletparams = new hashtable<>();
jerseyservletparams.put("javax.ws.rs.application",
restapplication.class.getname());
logger.info("kira2will" restapplication.class.getname());
return jerseyservletparams;
} }

statusservice.java

 package helloworldosgi;
import java.util.arraylist;
import java.util.list;
import java.util.logging.logger; import javax.ws.rs.get;
import javax.ws.rs.path;
import javax.ws.rs.pathparam;
import javax.ws.rs.produces;
import javax.ws.rs.core.mediatype; import data.node; @path("/status")
public class statusservice { private static final logger logger = logger.getlogger(statusservice.class.getname()); @get
@produces(mediatype.application_json)
public node getstatus(){ list nodes = new arraylist();
node node = new node("001", "60800","192.168.1.1","0","92","92","chizhou","50ms","hw");
node nothingnode = new node("null","null","null","null","null","null","null","null","null");
//nodes.add(node);
nodes.add(node);
logger.info(node.getname());
return node;
} @path("/{id}")
@get
@produces(mediatype.application_json)
public node getid(@pathparam("id") int id){
list nodes = new arraylist(); nodes.add(new node("null","null","null","null","null","null","null","null","null"));
nodes.add(new node("001", "60600","192.168.1.1","0","92","92","tonglin","50ms","hw"));
nodes.add(new node("002", "60600","192.168.1.1","0","92","92","tonglin","50ms","hw"));
nodes.add(new node("003", "60600","192.168.1.1","0","92","92","tonglin","50ms","hw"));
nodes.add(new node("004", "60600","192.168.1.1","0","92","92","tonglin","50ms","hw"));
nodes.add(new node("005", "60600","192.168.1.1","0","92","92","tonglin","50ms","hw")); int defaultindex = 0;
if ( (id < 1) || (id > nodes.size() - 1) ){
logger.info(nodes.get(defaultindex).getid());
return nodes.get(defaultindex);
}
else{
logger.info(nodes.get(id).getid());
return nodes.get(id);
}
}
}

restapplication.java

 package helloworldosgi;
 import java.util.hashset;
import java.util.set; import javax.ws.rs.core.application; import com.fasterxml.jackson.jaxrs.json.jacksonjsonprovider; public class restapplication extends application { @override
public set> getclasses() {
set> result = new hashset>(); result.add(jacksonjsonprovider.class); result.add(statusservice.class); return result;
}
}

node.java

 package data;
 import javax.xml.bind.annotation.xmlrootelement;
 @xmlrootelement
public class node { private string id;
private string name;
private string admin_ip;
private string admin_status;
private string longitude;
private string latitude;
private string location;
private string latency;
private string vendor_name; public node(string id,
string name,
string admin_ip,
string admin_status,
string longitude,
string latitude,
string location,
string latency,
string vendor_name
){
this.id = id;
this.name = name;
this.admin_ip = admin_ip;
this.admin_status = admin_status;
this.longitude = longitude;
this.latitude = latitude;
this.location = location;
this.latency = latency;
this.vendor_name = vendor_name;
}
public string getid() {
return id;
} public void setid(string id) {
this.id = id;
} public string getname() {
return name;
} public void setname(string name) {
this.name = name;
} public string getadmin_ip() {
return admin_ip;
} public void setadmin_ip(string admin_ip) {
this.admin_ip = admin_ip;
} public string getadmin_status() {
return admin_status;
} public void setadmin_status(string admin_status) {
this.admin_status = admin_status;
} public string getlongitude() {
return longitude;
} public void setlongitude(string longitude) {
this.longitude = longitude;
} public string getlatitude() {
return latitude;
} public void setlatitude(string latitude) {
this.latitude = latitude;
} public string getlocation() {
return location;
} public void setlocation(string location) {
this.location = location;
} public string getlatency() {
return latency;
} public void setlatency(string latency) {
this.latency = latency;
} public string getvendor_name() {
return vendor_name;
} public void setvendor_name(string vendor_name) {
this.vendor_name = vendor_name;
}
}

4.浏览器中输入 http://localhost:8080/rest/json/status 即可访问public node getstatus() 返回值。

或者输入 http://localhost:8080/rest/json/status/2 即可访问public node getid(@pathparam("id") int id)返回值。

关于@path @get @pathparam 参考 http://www.docin.com/p-317614298.html 第七页。

http://download.csdn.net/detail/kira_will/9729055

原创:equinox osgi应用嵌入jersey框架搭建rest服务的相关教程结束。

网站地图