![](/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.