![](/img/trans.png)
[英]JBoss + RestEasy + Jackson : org.jboss.resteasy.core.ServerResponse cannot be cast to org.jboss.resteasy.specimpl.BuiltResponse
[英]How to replace org.jboss.resteasy.core.ResourceMethodInvoker with Spring or POJO classes
我们有一个多年前使用 RESTEasy 开发的应用程序。 该实现使用 RestEasy 过滤器,该实现与此处显示的代码非常接近: RESTEasy ContainerRequestFilter – RESTEasy 安全过滤器示例我正在将该应用程序迁移到 Spring Boot,因为我们使用 Spring Boot 开发了所有其他应用程序。 我通过取出 JAX-RS 和 RESTEasy 并将 RESTEasy 过滤器替换为 Spring 过滤器来转换代码,类似于此处显示的代码:如何定义 Spring 引导过滤器? 我在当前实现中有代码,它检查如下方法的注释:
ResourceMethodInvoker methodInvoker = (ResourceMethodInvoker) requestContext.getProperty("org.jboss.resteasy.core.ResourceMethodInvoker");
Method method = methodInvoker.getMethod();
if(!method.isAnnotationPresent(PermitAll.class))
{
doSomething();
}
我正在寻找一些方法来使用 POJO 或 Spring 实现相同的方法验证逻辑,到目前为止我似乎还没有找到。 任何帮助将不胜感激。
谢谢。
在 spring 引导中,您可以使用拦截器。 在拦截器中,您将可以访问 HnadlerMethod,从中您可以知道调用了哪个服务方法以及它在哪个资源中,并且您可以获取天气注释是否存在。
步骤:创建一个处理程序拦截器并将其注册到 spring 上下文中,以便调用它。 在调用过滤器后调用处理程序拦截器。 它类似于 rest easy containerRequestFilter。 此外, javax.servlet.Filter
在containerFilter
之前被调用,这里也是同样的方式。
链接:https://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/HandlerInterceptor.html`
@Configuration
public class HandlerIntercepter extends HandlerInterceptorAdapter
{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse
response, Object handler) throws Exception
{
System.out.println("handler called");
HandlerMethod handlerMethod = (HandlerMethod) handler;
Class<?> clazz = handlerMethod.getBeanType();
Method m = handlerMethod.getMethod();
if (clazz != null)
{
boolean isClzAnnotation =
clazz.isAnnotationPresent(RequireSignInClassLevel.class);
}
if (m != null)
{
boolean isMethondAnnotation =
m.isAnnotationPresent(RequireSignIn.class);
}
return true;
}}
@Component
public class AppConfig extends WebMvcConfigurerAdapter
{
@Autowired
HandlerIntercepter HandlerIntercepter;
@Override
public void addInterceptors(InterceptorRegistry registry)
{
registry.addInterceptor(HandlerIntercepter);
}
}`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.