gpt4 book ai didi

java - 使用 Spring REST 服务的拦截器

转载 作者:行者123 更新时间:2023-11-30 06:39:04 24 4
gpt4 key购买 nike

I have implemented a rest webservice using Spring.My RestController code is as given below.I have created the interceptor as you have suggested in the above post.I am able to get the rest response but the requests are not getting intercepted.Could you please help me figure out what's wrong with my code:`

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 ;


}


}

I have added config file as below.Still it's not working:

@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;
}
});
}
}

最佳答案

您需要使用注释您的配置类

@EnableWebMvc
@Configuration

然后使用@ComponentScan扫描配置类的包

关于java - 使用 Spring REST 服务的拦截器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44707466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com