Jersey JAX-RS 独立服务器 - 如何将参数传递给请求处理程序?
Jersey JAX-RS standalone server - how pass parameters to request handler?
我构建了一个简单的 Jersey rest 服务器,用于处理简单的服务。
在启动服务器的 main class 中,我读取了我想要的属性文件
provide/pass 到 jax 处理程序 classes。
服务器工作,我只需要想办法共享配置参数
main class 带有请求处理程序。
我该怎么做?
main代码,我只读了一次de属性文件:
...
public HashMap resources;
// this start the listener jersey server...
String host="http://localhost/";
int port = 9998;
URI baseUri = UriBuilder.fromUri(host).port(port).build();
ResourceConfig config = new ResourceConfig();
config.packages(true, "br.com.myserver");
config.register(MyHandler.class);
// I WANT TO ACCESS/SHARE THIS WITH THE HANDLER -> MyHandler.class
resources.put("host_user","bla bla bla");
HttpServer server = JdkHttpServerFactory.createHttpServer(baseUri, config);
System.out.println("--Press Enter to STOP the server--");
System.in.read();
Server.stop(0);
System.out.println("Server stoped!");
...
MyHandler 的代码,我想在其中访问主要属性:
@Path("myapp")
public class MyHandler
{
@POST @Path("/testep")
@Consumes("application/json")
@Produces("text/plain")
public String action1(@Context Request request, String json)
{
// HERE I WANT TO ACCESS THE RESOUCES HASHMAP OF MAIN HERE
// (how get main handler here).resources.get("host_user");
// maybe access main class, or something like
// the intention is to avoid the read of config at all requests here
System.out.println("received event:" + json);
return "event received " + json;
}
}
任何想法将不胜感激,谢谢。
您使用 property(key, value)
配置 ResourceConfig
的任何属性都可以通过 Configuration
接口访问,您可以将其注入资源 class.
ResourceConfig config = new ResourceConfig();
config.property("host_user","bla bla bla");
...
@Path("myapp")
public class MyHandler
{
@Context
Configuration configuration;
public String action1(@Context Request request, String json) {
Map<String, Object> props = configuration.getProperties();
}
}
另请参阅:
我构建了一个简单的 Jersey rest 服务器,用于处理简单的服务。 在启动服务器的 main class 中,我读取了我想要的属性文件 provide/pass 到 jax 处理程序 classes。 服务器工作,我只需要想办法共享配置参数 main class 带有请求处理程序。 我该怎么做?
main代码,我只读了一次de属性文件:
...
public HashMap resources;
// this start the listener jersey server...
String host="http://localhost/";
int port = 9998;
URI baseUri = UriBuilder.fromUri(host).port(port).build();
ResourceConfig config = new ResourceConfig();
config.packages(true, "br.com.myserver");
config.register(MyHandler.class);
// I WANT TO ACCESS/SHARE THIS WITH THE HANDLER -> MyHandler.class
resources.put("host_user","bla bla bla");
HttpServer server = JdkHttpServerFactory.createHttpServer(baseUri, config);
System.out.println("--Press Enter to STOP the server--");
System.in.read();
Server.stop(0);
System.out.println("Server stoped!");
...
MyHandler 的代码,我想在其中访问主要属性:
@Path("myapp")
public class MyHandler
{
@POST @Path("/testep")
@Consumes("application/json")
@Produces("text/plain")
public String action1(@Context Request request, String json)
{
// HERE I WANT TO ACCESS THE RESOUCES HASHMAP OF MAIN HERE
// (how get main handler here).resources.get("host_user");
// maybe access main class, or something like
// the intention is to avoid the read of config at all requests here
System.out.println("received event:" + json);
return "event received " + json;
}
}
任何想法将不胜感激,谢谢。
您使用 property(key, value)
配置 ResourceConfig
的任何属性都可以通过 Configuration
接口访问,您可以将其注入资源 class.
ResourceConfig config = new ResourceConfig();
config.property("host_user","bla bla bla");
...
@Path("myapp")
public class MyHandler
{
@Context
Configuration configuration;
public String action1(@Context Request request, String json) {
Map<String, Object> props = configuration.getProperties();
}
}
另请参阅: