我使用spring和resteasy集成来暴露一些基于REST的服务。我打算使用拦截器进行日志记录和身份验证,但面临拦截器调用的问题。
控制器类(使用@Path作为休息服务)
@Controller
@Path("/v1.0/create")
public class ContollerClass{
@POST
@Path("/artifact")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String someMethod(SampleVO sampleVO,@Context HttpServletRequest request) {
// Some functionality
}
acctrest-servlet.xml:
<context:component-scan base-package="com.comp.abc.xyz" />
<import resource="classpath:springmvc-resteasy.xml" />
<mvc:interceptors>
<bean class="com.comp.abc.xyz.interceptor.AuthenticationInterceptor" />
</mvc:interceptors>
拦截器:
public class AuthenticationInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2) throws Exception {
System.out.println("pre handle ************************");
log.info("before handler execution");
String requestID = Long.toString(System.currentTimeMillis());
log.debug("request ID is {} :: ", requestID);
return true;
}
Web.xml中
<servlet>
<servlet-name>acctrest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>acctrest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
除了拦截器调用之外,其余的流程都工作正常。