在被调用的方法中访问 iriInfo
Access iriInfo in called Method
此代码可用于访问 uriInfo:
@Path("/testing")
public class Testing {
@javax.ws.rs.core.Context UriInfo uriInfo;
@POST
@Path("/test2")
@Produces(MediaType.TEXT_PLAIN)
public Response test2(
@FormParam("sessionId") String sessionId ) {
String currentUserId = Utils.setup(sessionId);
String accessPath = uriInfo.getAbsolutePath().toASCIIString();
System.out.println("The client used this URI to reach this resource method: " + uriInfo.getAbsolutePath().toASCIIString());
// Utils.test3("print this");
return Response.ok("Test 2 ended").build();
}
当我尝试访问调用方法 Utils.test3("print this") 中的 uriInfo 时;这里:
public class Utils {
@javax.ws.rs.core.Context static UriInfo uriInfo;
public static String setup(String sessionId) {
if (!com.retailapppartners.Utils.validSession(sessionId)) {
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
String currentUserId = com.retailapppartners.Utils.getUserFromSession(sessionId);
MDC.put("user-id", currentUserId);
return currentUserId;
}
public static void test3(String message) {
System.out.println(message);
String path = uriInfo.getPath();
// System.out.println("The client used this URI: " + uriInfo.getAbsolutePath().toASCIIString());
return;
}
由于空指针异常而失败。我想在被调用的方法中查看路径 uri,以确认我的 utils 中所有方法的安全性。我已经在 hi 和 low 中搜索了这方面的例子。谢谢
使用@Context 注释将 UriInfo 实例注入资源的字段变量或方法参数中class
例如#1
public String find(@Context UriInfo uri){}
例如#2
public class RESTResource{
@Context
private UriInfo uri;
}
继续我的评论..变成一个答案
Like I said, you can't just decide to inject it anywhere you want. The class being injected into needs to be managed by the JAX-RS runtime, as it's the one that will be doing the injecting. A resource class is managed, a filter provider is managed, that's why you can inject into them. You're utility class is not. And in any case, I don't think it would even work for a [static] "utility" class (even if you were to somehow get it managed) because of the static nature.
首先让我提一下,UriInfo
是针对每个请求的。 static
,本质上就是"global"。您不能将其作为静态字段注入。
我能看到的一个解决方案是使 Utils
class(和方法)成为非静态的,并使用底层注入框架注入 Utils
class,随时随地。这样,如果 Utils
class 是托管的,那么它应该能够注入托管的 UriInfo
实例。如何实现这一点(让 Utils
class 得到管理)取决于您使用的实现及其底层注入框架。
例如,对于 Jersey (2),我可以这样做
public class Utils {
@Context UriInfo uriInfo;
public String test(String s) {
return s + "=" +uriInfo.getAbsolutePath().toString();
}
}
@Path("some")
public class SomeResource {
@Inject
Utils utils;
@GET
public Response getSomething() {
return Response.ok(utils.test("Hello")).build();
}
}
public class JerseyApplication extends ResourceConfig {
public JerseyApplication() {
packages("Whosebug.jersey.test");
register(new AbstractBinder(){
@Override
protected void configure() {
bind(Utils.class).to(Utils.class);
}
});
}
}
这很好用
C:\>curl -v http://localhost:8080/some
Result: Hello=http://localhost:8080/some
Jersey 使用 HK2 进行注入,因此我可以利用它来注入我的 Utils
class.
现在这可能不是您正在寻找的答案(因为它违背了静态实用程序 class 的目的),但是您正在尝试的也无法完成。无论采用哪种方式,无论是重构以将 UriInfo
传递给静态方法,还是上述解决方案,您都可能需要进行大量重构。我很惊讶你已经使用这个功能创建了 200 个方法,然后才确保其中一个有效:/
此代码可用于访问 uriInfo:
@Path("/testing")
public class Testing {
@javax.ws.rs.core.Context UriInfo uriInfo;
@POST
@Path("/test2")
@Produces(MediaType.TEXT_PLAIN)
public Response test2(
@FormParam("sessionId") String sessionId ) {
String currentUserId = Utils.setup(sessionId);
String accessPath = uriInfo.getAbsolutePath().toASCIIString();
System.out.println("The client used this URI to reach this resource method: " + uriInfo.getAbsolutePath().toASCIIString());
// Utils.test3("print this");
return Response.ok("Test 2 ended").build();
}
当我尝试访问调用方法 Utils.test3("print this") 中的 uriInfo 时;这里:
public class Utils {
@javax.ws.rs.core.Context static UriInfo uriInfo;
public static String setup(String sessionId) {
if (!com.retailapppartners.Utils.validSession(sessionId)) {
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
String currentUserId = com.retailapppartners.Utils.getUserFromSession(sessionId);
MDC.put("user-id", currentUserId);
return currentUserId;
}
public static void test3(String message) {
System.out.println(message);
String path = uriInfo.getPath();
// System.out.println("The client used this URI: " + uriInfo.getAbsolutePath().toASCIIString());
return;
}
由于空指针异常而失败。我想在被调用的方法中查看路径 uri,以确认我的 utils 中所有方法的安全性。我已经在 hi 和 low 中搜索了这方面的例子。谢谢
使用@Context 注释将 UriInfo 实例注入资源的字段变量或方法参数中class
例如#1
public String find(@Context UriInfo uri){}
例如#2
public class RESTResource{
@Context
private UriInfo uri;
}
继续我的评论..变成一个答案
Like I said, you can't just decide to inject it anywhere you want. The class being injected into needs to be managed by the JAX-RS runtime, as it's the one that will be doing the injecting. A resource class is managed, a filter provider is managed, that's why you can inject into them. You're utility class is not. And in any case, I don't think it would even work for a [static] "utility" class (even if you were to somehow get it managed) because of the static nature.
首先让我提一下,UriInfo
是针对每个请求的。 static
,本质上就是"global"。您不能将其作为静态字段注入。
我能看到的一个解决方案是使 Utils
class(和方法)成为非静态的,并使用底层注入框架注入 Utils
class,随时随地。这样,如果 Utils
class 是托管的,那么它应该能够注入托管的 UriInfo
实例。如何实现这一点(让 Utils
class 得到管理)取决于您使用的实现及其底层注入框架。
例如,对于 Jersey (2),我可以这样做
public class Utils {
@Context UriInfo uriInfo;
public String test(String s) {
return s + "=" +uriInfo.getAbsolutePath().toString();
}
}
@Path("some")
public class SomeResource {
@Inject
Utils utils;
@GET
public Response getSomething() {
return Response.ok(utils.test("Hello")).build();
}
}
public class JerseyApplication extends ResourceConfig {
public JerseyApplication() {
packages("Whosebug.jersey.test");
register(new AbstractBinder(){
@Override
protected void configure() {
bind(Utils.class).to(Utils.class);
}
});
}
}
这很好用
C:\>curl -v http://localhost:8080/some
Result:Hello=http://localhost:8080/some
Jersey 使用 HK2 进行注入,因此我可以利用它来注入我的 Utils
class.
现在这可能不是您正在寻找的答案(因为它违背了静态实用程序 class 的目的),但是您正在尝试的也无法完成。无论采用哪种方式,无论是重构以将 UriInfo
传递给静态方法,还是上述解决方案,您都可能需要进行大量重构。我很惊讶你已经使用这个功能创建了 200 个方法,然后才确保其中一个有效:/