Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 拦截请求&;发送到外部_Spring Boot_Spring Mvc - Fatal编程技术网

Spring boot 拦截请求&;发送到外部

Spring boot 拦截请求&;发送到外部,spring-boot,spring-mvc,Spring Boot,Spring Mvc,我正在开发一个Spring boot项目 我的控制器的一个示例: @Controller public class RestController { @GetMapping(value = "/student/{studentId}") public @ResponseBody Student getData(@PathVariable Integer studentId) { Student student = new Student(); s

我正在开发一个Spring boot项目

我的控制器的一个示例:

@Controller
public class RestController {

    @GetMapping(value = "/student/{studentId}")
    public @ResponseBody Student getData(@PathVariable Integer studentId) {
        Student student = new Student();
        student.setName("Peter");
        student.setId(studentId);

        return student;
    }
}
我还实现了其他端点

我需要截获每个到达我端点的请求并将请求转发给另一个服务(microservice),换句话说,我需要根据该服务的响应将每个请求转发给与当前web应用运行在同一台本地计算机上的另一个web应用,以决定是否继续转发请求


我的粗略想法是使用,但我不确定我是否走对了方向。有人能分享一些经验吗?实现这一目标的最佳方式是什么?如果您能展示一些示例代码,那就太好了。提前感谢。

您可以使用HandlerInterceptor适配器

定义拦截器如下

@Component
public class RequestInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object object) throws Exception {
        System.out.println("In preHandle we are Intercepting the Request");
        System.out.println("____________________________________________");
        //Call the Rest API and Validate 
        if (conditionsNotMet()) {
           response.getWriter().write("something");
           response.setStatus(someErrorCode);

           return false;
        }
    }

}
注册HandlerInterceptorAdapter

@Configuration
public class PathMatchingConfigurationAdapter extends WebMvcConfigurerAdapter {

    @Autowired
    private RequestInterceptor requestInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(requestInterceptor);
    }
}
使用WebMVCConfiguer

@Configuration
public class PathMatchingConfigurationAdapter implements WebMvcConfigurer {

    @Autowired
    private RequestInterceptor requestInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(requestInterceptor);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry resourceHandlerRegistry) {

    }

    @Override
    public void addCorsMappings(CorsRegistry corsRegistry) {

    }

    @Override
    public void addViewControllers(ViewControllerRegistry viewControllerRegistry) {

    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry viewResolverRegistry) {

    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> list) {

    }

    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> list) {

    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> list) {

    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> list) {

    }

    @Override
    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> list) {

    }

    @Override
    public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> list) {

    }

    @Override
    public Validator getValidator() {
        return null;
    }

    @Override
    public MessageCodesResolver getMessageCodesResolver() {
        return null;
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer asyncSupportConfigurer) {

    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer defaultServletHandlerConfigurer) {

    }

    @Override
    public void addFormatters(FormatterRegistry formatterRegistry) {

    }

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(false);
    }
}
@配置
公共类PathMatchingConfigurationAdapter实现WebMVCConfiguer{
@自动连线
私有请求截取器请求截取器;
@凌驾
公共无效附加接收器(侦听器注册表){
addInterceptor(requestInterceptor);
}
@凌驾
public void addResourceHandlers(ResourceHandlerRegistry ResourceHandlerRegistry){
}
@凌驾
公共无效添加公司标志(公司注册公司注册){
}
@凌驾
public void addViewController(ViewControllerRegistry ViewControllerRegistry){
}
@凌驾
公共无效配置ViewResolver(ViewResolverRegistry ViewResolverRegistry){
}
@凌驾
公共无效AddArgumentResolver(列表){
}
@凌驾
public void addReturnValueHandler(列表){
}
@凌驾
public void configureMessageConverters(列表>列表){
}
@凌驾
公共无效配置HandlerExceptionResolver(列表){
}
@凌驾
公共无效ExtendHandlerExceptionResolver(列表){
}
@凌驾
公共验证器getValidator(){
返回null;
}
@凌驾
public MessageCodesResolver getMessageCodesResolver(){
返回null;
}
@凌驾
public void配置contentnegotiation(ContentNegotiationConfigurer-configurer){
configurer.favorPathExtension(false);
}
@凌驾
public void configureAsyncSupport(AsyncSupportConfigurer AsyncSupportConfigurer){
}
@凌驾
公共无效配置DefaultServletHandler处理(DefaultServletHandlerConfigurer DefaultServletHandlerConfigurer){
}
@凌驾
公共void addFormatters(FormatterRegistry FormatterRegistry){
}
@凌驾
公共无效配置路径匹配(路径匹配配置器配置器){
configurer.setUseSuffixPatternMatch(false);
}
}

谢谢。是的,我知道如何使用HandlerInterceptorAdapter,但是1。如何将请求转发到其他服务?2、如何根据来自其他服务的响应在响应为真时转发请求对象或在响应为假时丢弃请求(假设其他服务对此请求的响应是布尔标志)?您可以使用HttpServletRequest获取输入,并在preHandle方法内进行rest API调用。根据验证,您可以返回true或false。您的意思是在reHandle方法中,return true将继续处理请求,return false将放弃请求?如果preHandle方法返回'false',Spring框架假定请求已由Spring拦截器本身处理,不需要进一步处理。在这种情况下,我们应该使用response对象向客户端请求发送响应。是的,没错,所以,我的问题是如何基于其他服务响应布尔值向客户端响应响应对象?