java过滤器获取uri,java – Dropwizard请求使用URI模式的过滤器

我正在Dropwizard中构建一个RESTful应用程序.连接到数据库时,我想设置一个实现ContainerRequestFilter的UserNotFoundFilter,以便传入的请求首先通过此过滤器.

我的想法是,我希望这个特定的过滤器只映射到某些URI模式.例如,我希望过滤器仅适用于/ users / *而不是其他任何内容.有没有办法在不使用DynamicFeature的自定义注释和实现的情况下执行此操作?

@Provider

public class UserNotFoundFilter implements ContainerRequestFilter {

@Context

UriInfo uriInfo;

@Override

public void filter(ContainerRequestContext requestContext) throws IOException {

MultivaluedMap pathParams = uriInfo.getPathParameters(); // Should contain (uid: 1) pair for /users/1

boolean userExists = // check against the database using above pathparam pair to see if user exists

if (!userExists)

throw new WebApplicationException("User does not exist", Status.NOT_FOUND);

// let the request through as user exists

}

}

我的UserResource类

public class UserResource {

@GET

@Path("/users/{uid}")

@Produces(MediaType.APPLICATION_JSON)

public User getUser(@PathParam("uid") String uid) {

// Now I don't need to do the check here !

// boolean userExists = check against database using uid path param

// if (!userExists)

// throw new WebApplicationException("User does not exist", Status.NOT_FOUND);

return database.getUser(uid);

}

}

我的ItemResource类

public class ItemResource {

@GET

@Path("/items/{uid}")

@Produces(MediaType.APPLICATION_JSON)

public Item getItem(@PathParam("uid") String uid) {

return database.getItem(uid);

}

}

我想做什么

public class MyApplication extends Application {

// ...

@Override

public void run(MyConfiguration config, Environment environment) throws Exception {

// ... do other things, register resources

// this pseudocode, the UserNotFoundFilter only applies to URIs of the kind /users/*

environment.jersey().register(new UserNotFoundFilter()).forUriPattern("/users/*");

我感谢任何示例代码片段.

最佳答案 对于Servlet过滤器 –

可能你正在寻找的是来自javax.servlet.FilterRegistration接口的addMappingForUrlPatterns,用于你的run()as –

environment.servlets().addFilter("FilterName", UserNotFoundFilter.class)

.addMappingForUrlPatterns(EnumSet

.allOf(DispatcherType.class), true, "/users/*");

上述方法的签名是 –

public void addMappingForUrlPatterns(

EnumSet dispatcherTypes, boolean isMatchAfter,

String... urlPatterns);

编辑 – 动态绑定:

尝试使用DynamicFeature作为

@Provider

public class UserNotFoundDynamicFilter implements DynamicFeature {

@Override

public void configure(ResourceInfo resourceInfo, FeatureContext featureContext) {

if (resourceInfo.getResourceMethod().getAnnotation(UserRequired.class) != null) {

featureContext.register(UserNotFoundFilter.class);

}

}

}

您可以在其中将UserRequired注释定义为 –

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

public @interface UserRequired {

}

并在您的资源中标记所有/ users / * apis具有相同的注释 –

@GET

@Path("/users/{uid}")

@Produces(MediaType.APPLICATION_JSON)

@UserRequired

public User getUser(@PathParam("uid") String uid) {

// Now I don't need to do the check here !

// boolean userExists = check against database using uid path param

// if (!userExists)

// throw new WebApplicationException("User does not exist", Status.NOT_FOUND);

return database.getUser(uid);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值