将变量传递给 Jersey 中的资源 class
Pass variable to resource class in Jersey
我正在尝试使用一个变量,该变量通过命令行参数传递给主函数,并且应该以某种方式在球衣的资源 class 中可见。
我的主要功能:
public class MyApp extends ResourceConfig {
public MyApp(String directory) {
// I would like the MyResource.class to have access to the
// variable that is passed in the main function below,
// which is the directory variable
register(MyResurce.class);
}
public void startHttpServer(int port) { ... }
// args[0]: a port to start a HTTP server
// args[1]: a string that is CONSTANT and UNIQUE throughout the
// full execution of the app, and the MyResource.class resource
// should be able to read it. How can I pass this variable to the resource?
public static void main(String[] args) {
final MyApp app = new MyApp(args[1]);
int port = Integer.parseInt(args[0]);
app.startHttpServer(port);
}
}
资源class没有什么特别之处,只有@GET、@DELETE和@POST方法。我应该怎么做才能使 args[1] 中给出的变量不仅对 MyResource.class 可见,而且对所有已注册的资源可见?
如果您在资源 class 中注入 JAX-RS
应用程序,您可以访问属性 Map
:
@Path(...)
public class MyResource {
@Context
private Application app;
@GET
@Path(...)
public String someMethod() {
String directory = app.getProperties().get("directory");
...
}
}
那么,你的主要 class 会是这样的:
public class MyApp extends ResourceConfig {
public MyApp(String directory) {
register(MyResource.class);
Map<String, Object> properties = new HashMap<>();
properties.put("directory", directory);
setProperties(properties);
}
public void startHttpServer(int port) { ... }
public static void main(String[] args) {
final MyApp app = new MyApp(args[1]);
int port = Integer.parseInt(args[0]);
app.startHttpServer(port);
}
}
您可以执行上述操作,因为 ResourceConfig
扩展了 Application
。
我正在尝试使用一个变量,该变量通过命令行参数传递给主函数,并且应该以某种方式在球衣的资源 class 中可见。
我的主要功能:
public class MyApp extends ResourceConfig {
public MyApp(String directory) {
// I would like the MyResource.class to have access to the
// variable that is passed in the main function below,
// which is the directory variable
register(MyResurce.class);
}
public void startHttpServer(int port) { ... }
// args[0]: a port to start a HTTP server
// args[1]: a string that is CONSTANT and UNIQUE throughout the
// full execution of the app, and the MyResource.class resource
// should be able to read it. How can I pass this variable to the resource?
public static void main(String[] args) {
final MyApp app = new MyApp(args[1]);
int port = Integer.parseInt(args[0]);
app.startHttpServer(port);
}
}
资源class没有什么特别之处,只有@GET、@DELETE和@POST方法。我应该怎么做才能使 args[1] 中给出的变量不仅对 MyResource.class 可见,而且对所有已注册的资源可见?
如果您在资源 class 中注入 JAX-RS
应用程序,您可以访问属性 Map
:
@Path(...)
public class MyResource {
@Context
private Application app;
@GET
@Path(...)
public String someMethod() {
String directory = app.getProperties().get("directory");
...
}
}
那么,你的主要 class 会是这样的:
public class MyApp extends ResourceConfig {
public MyApp(String directory) {
register(MyResource.class);
Map<String, Object> properties = new HashMap<>();
properties.put("directory", directory);
setProperties(properties);
}
public void startHttpServer(int port) { ... }
public static void main(String[] args) {
final MyApp app = new MyApp(args[1]);
int port = Integer.parseInt(args[0]);
app.startHttpServer(port);
}
}
您可以执行上述操作,因为 ResourceConfig
扩展了 Application
。