拦截器与Spring REST服务

时间:2017-06-22 19:04:02

标签: java spring rest

  

我已经使用Spring实现了一个rest webservice.My RestController代码如下所示。我已经按照你在上面的帖子中建议的那样创建了拦截器。我能够得到其余的响应,但请求没有被截获。你能不能帮我弄清楚我的代码有什么问题:`

import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
class WebConfig extends WebMvcConfigurerAdapter {

       @Override
       public void addInterceptors(InterceptorRegistry registry) {
          registry.addInterceptor(new HandlerInterceptorAdapter() {

             Logger logger = LoggerFactory
                    .getLogger(WebConfig.class);
             @Override
             public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                if (handler instanceof HandlerMethod) {
                     logger.info("Request URL::" );
                   HandlerMethod handlerMethod = (HandlerMethod) handler;
              //     Method method = handlerMethod.getMethod();
               /**    logger.info("{} - {} - method '{}' on controller '{}'",
                         request.getMethod(), request.getRequestURI(), method.getName(),
                         handlerMethod.getBean().getClass()
                   );*/
                   logger.info("Request URL::" + request.getRequestURL().toString()
                        + ":: Start Time=" + System.currentTimeMillis());
                }
                return true;
             }
          });
       }
    }

`

 @RestController
    @RequestMapping(/v1/search)
    public class RestController {   
        @Autowired
        @Qualifier("repoService")
        private IRepoService repoService;
        private static final Logger logger = Logger.getLogger( RestController.class );
        @RequestMapping(value = "/{id}" ,method = RequestMethod.GET)    

        public List<RepVO> getRepById(@PathVariable String id) 
            List<RepVO> repVOList = repoService.getRepById(id);
            logger.info("Response received");    

            return repVOList ;


        }


    }
  

我已经添加了配置文件,如下所示。它还没有工作:

@EnableWebMvc
@Configuration
class WebConfig extends WebMvcConfigurerAdapter {

       @Override
       public void addInterceptors(InterceptorRegistry registry) {
          registry.addInterceptor(new HandlerInterceptorAdapter() {

             Logger logger = LoggerFactory
                    .getLogger(WebConfig.class);
             @Override
             public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                if (handler instanceof HandlerMethod) {
                     logger.info("Request URL::" );
                   HandlerMethod handlerMethod = (HandlerMethod) handler;
              //     Method method = handlerMethod.getMethod();
               /**    logger.info("{} - {} - method '{}' on controller '{}'",
                         request.getMethod(), request.getRequestURI(), method.getName(),
                         handlerMethod.getBean().getClass()
                   );*/
                   logger.info("Request URL::" + request.getRequestURL().toString()
                        + ":: Start Time=" + System.currentTimeMillis());
                }
                return true;
             }
          });
       }
    }

1 个答案:

答案 0 :(得分:1)

您需要使用

注释您的配置类
@EnableWebMvc
@Configuration

然后使用@ComponentScan

扫描配置类的包