如何从 java 中的另一项服务调用一项球衣服务?另外如何在我的 AuthenticationFilter 中注入 HttpServletRequest 对象
How can I call one jersey service from another service in java ? Also how can I inject HttpServletRequest object inside my AuthenticationFilter
在 Jersey 2.0 中注入 HttpServletRequest 对象时出现异常 -> java.lang.IllegalStateException: Not inside a request scope
.
我尝试在 AuthenticationFIlter 中注入 HttpServletRequest 对象以获取客户端 IP 地址,但由于上述异常而失败,但相同的代码在 jersey 2.2.3 api 中完美运行。
import javax.annotation.Priority;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Priorities;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.Provider;
@Provider
@Priority(Priorities.AUTHENTICATION)
public class AuthFilter implements ContainerRequestFilter {
@Context
HttpServletRequest request;
public String getClientIP(@Context HttpServletRequest request) {
return request.getRemoteAddr();
}
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
System.out.println("Filter called.");
clientIP = new AuthFilter().getClientIP(request);
}
}
你的问题有点令人费解。您提到您使用的是 Jersey 2.x,但您实际上使用的是 Jersey 1.x API。
Jersey 1.x 和 Jersey 2.x 使用不同的包名:
- 球衣 1.x:
com.sun.jersey
- 球衣 2.x:
org.glassfish.jersey
在泽西岛 2.x,您将拥有以下内容:
import java.io.IOException;
import javax.annotation.Priority;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Priorities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;
@Provider
@Priority(Priorities.AUTHENTICATION)
public class AuthenticationFilter implements ContainerRequestFilter {
@Context
private HttpServletRequest request;
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
String remoteAddress = request.getRemoteAddr();
...
}
}
ContainerRequestFilter
interface is in the javax.ws.rs.container
包,它是 JAX-RS 2.0 API 的一部分。而 Jersey 2.x 是 JAX-RS 2.0 规范的参考实现。
在 Jersey 2.0 中注入 HttpServletRequest 对象时出现异常 -> java.lang.IllegalStateException: Not inside a request scope
.
我尝试在 AuthenticationFIlter 中注入 HttpServletRequest 对象以获取客户端 IP 地址,但由于上述异常而失败,但相同的代码在 jersey 2.2.3 api 中完美运行。
import javax.annotation.Priority;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Priorities;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.Provider;
@Provider
@Priority(Priorities.AUTHENTICATION)
public class AuthFilter implements ContainerRequestFilter {
@Context
HttpServletRequest request;
public String getClientIP(@Context HttpServletRequest request) {
return request.getRemoteAddr();
}
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
System.out.println("Filter called.");
clientIP = new AuthFilter().getClientIP(request);
}
}
你的问题有点令人费解。您提到您使用的是 Jersey 2.x,但您实际上使用的是 Jersey 1.x API。
Jersey 1.x 和 Jersey 2.x 使用不同的包名:
- 球衣 1.x:
com.sun.jersey
- 球衣 2.x:
org.glassfish.jersey
在泽西岛 2.x,您将拥有以下内容:
import java.io.IOException;
import javax.annotation.Priority;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Priorities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;
@Provider
@Priority(Priorities.AUTHENTICATION)
public class AuthenticationFilter implements ContainerRequestFilter {
@Context
private HttpServletRequest request;
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
String remoteAddress = request.getRemoteAddr();
...
}
}
ContainerRequestFilter
interface is in the javax.ws.rs.container
包,它是 JAX-RS 2.0 API 的一部分。而 Jersey 2.x 是 JAX-RS 2.0 规范的参考实现。