我已经在一年中的大部分时间里使用了泽西,并且偶然发现了一个我无法找到答案的问题:你如何拦截(或挂钩)泽西岛请求生命周期?
理想情况下,我可以在容器接受来自网络的请求和调用处理程序方法的时间之间执行一些自定义过滤/验证/拒绝.如果有一种通过子路径过滤拦截器的简单方法(例如,对于/下的任何内容都有一个拦截器,对于/ user /下的任何东西,等等),可以获得奖励积分.
谢谢!
编辑:为了更清楚一点,这里的一般想法是能够编写一些将为许多API调用运行的代码,而无需从每个处理程序方法显式调用该代码.这将减少额外的代码并消除传递请求上下文的需要.
正如在如何使用Jersey拦截器来获取请求主体中所讨论的那样,我正在修改ContainerRequestFilter中的EntityInputStream.
public filter(ContainerRequest request){
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = request.getEntityInputStream();
try{
Readerwriter.writeTo(in, out);
byte[] requestEntity = out.toByteArray();
// DO SOMETHING WITH BYTES HERE
request.setEntityInputStream(new ByteArrayInputStream(requestEntity));
}/// error handling code here
}
Run Code Online (Sandbox Code Playgroud)
但是,后来我无法弄清楚如何访问修改后的InputStream.我可以在资源中获取ServletContext,但我无法弄清楚如何获取我在过滤器中实际修改的对象ContainerRequest.
我可以这样做吗?当我尝试这个时,泽西岛无法启动:
@Post
@Path("/test")
public Response test(@Context ContainerRequest cr){
// blah blah
return....
}
Run Code Online (Sandbox Code Playgroud)
泽西岛错误:
方法public javax.ws.rs.core.Response example.TestController.test(com.sun.jersey.spi.container.ContainerRequest)缺少依赖性,用资源的POST注释,类example.TestController,不被认为是有效的资源方法.
我被困在一个旧版本的球衣,1.8,所以我不确定这是否是问题的一部分.
我是春季和球衣的新人。我正在尝试构建一个过滤器来检查请求是否具有正确的参数。这是我要检查的 json 部分:
"request":{
"application":"1 Android Mobile",
"version":1
}
Run Code Online (Sandbox Code Playgroud)
数据元素是我的 VersionDTO 的一部分:
public class VersionDTO {
int version;
String application;
public String getApplication() {
return application;
}
public void setApplication(String application) {
this.application = application;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的过滤器的代码:
@Override
public void filter(ContainerRequestContext requestContext) throws FilterException {
if (isSecureURL(requestContext.getUriInfo().getPath())){}{
try {
versionFilterService.setVersionDTO(//here is where I want to pass the VersionDTO object with the data of …Run Code Online (Sandbox Code Playgroud)