Tôi có một thời gian khó hiểu cơ chế tiêm của Jersey. Đặc tả JAX-RS (http://jsr311.java.net/nonav/releases/1.1/spec/spec3.html#x3-520005) nói rằng việc tiêm thông qua @Context có thể có trong các lớp con ứng dụng, các lớp tài nguyên gốc và các nhà cung cấp.Jersey @ Phạm vi tiếp theo
Tôi hiện có một lớp được khởi tạo ngay khi khởi động và có phương thức được gọi theo mọi yêu cầu. Bên trong phương thức tôi cần truy cập đối tượng UriInfo hiện tại. Vấn đề là, phương pháp này không được gọi từ mã của tôi. Vì vậy, tôi không thể vượt qua UriInfo trực tiếp đến phương pháp.
Tôi thực sự muốn làm một cái gì đó như thế này:
public class MyClass implements ThirdPartyInterface {
// not possible because class is no Application subclass, root resource class or provider
@Context
private UriInfo uriInfo;
public void methodCallebByThirdPartyCode() {
Uri requestUri = uriInfo.getRequestUri();
// do something
}
}
Tôi cố gắng này. Rõ ràng là không thành công:
public class MyClass implements ThirdPartyInterface {
private UriInfo uriInfo;
public MyClass(UriInfo uriInfo) {
this.uriInfo = uriInfo;
}
public void methodCallebByThirdPartyCode() {
Uri requestUri = uriInfo.getRequestUri();
// do something
}
}
@Provider
@Produces(MediaType.WILDCARD)
public class MyBodyWriter implements MessageBodyWriter<MyView> {
@Context
private UriInfo uriInfo;
private MyClass myClass;
private ThirdPartyClass thirdPartyClass;
public MyBodyWriter() {
// uriInfo is null at this time :(
myClass = new MyClass(uriInfo);
thirdPartyClass = new ThirdPartyClass();
thirdPartyClass.register(myClass);
}
public void writeTo(final MyView view, final Class<?> type, /* and so on */) throws IOException, WebApplicationException {
// execute() calls MyClass#methodCallebByThirdPartyCode()
thirdPartyClass.execute();
}
}
Cách giải quyết duy nhất tôi có thể nghĩ là đây. Tôi không nghĩ rằng nó rất sạch sẽ:
public class MyClass implements ThirdPartyInterface {
private UriInfo uriInfo;
public void setUriInfo(final UriInfo uriInfo) {
this.uriInfo = uriInfo;
}
public void methodCallebByThirdPartyCode() {
Uri requestUri = uriInfo.getRequestUri();
// do something
}
}
@Provider
@Produces(MediaType.WILDCARD)
public class MyBodyWriter implements MessageBodyWriter<MyView> {
@Context
private UriInfo uriInfo;
private MyClass myClass;
private ThirdPartyClass thirdPartyClass;
public MyBodyWriter() {
myClass = new MyClass();
thirdPartyClass = new ThirdPartyClass();
thirdPartyClass.register(myClass);
}
public void writeTo(final MyView view, final Class<?> type, /* and so on */) throws IOException, WebApplicationException {
myClass.setUriInfo(uriInfo);
// execute() calls MyClass#methodCallebByThirdPartyCode()
thirdPartyClass.execute();
myClass.setUriInfo(null);
}
}
Tôi hy vọng có giải pháp tốt hơn, nhưng có lẽ tôi hoàn toàn đi sai hướng.
Cảm ơn!
Có thể bạn chỉ cần 'ContainerRequestFilter'? – Willy
Tôi không biết nếu điều này làm việc trong tình huống của tôi. Đặc điểm kỹ thuật nói rằng "Các lớp nhà cung cấp được khởi tạo bởi thời gian chạy JAX-RS". Nhưng tôi cần một tham chiếu đến đối tượng tại thời điểm xây dựng, để chuyển nó tới dịch vụ của bên thứ ba. –