简体   繁体   English

自动配置 RestController Spring 启动

[英]AutoConfigure RestController Spring Boot

I have tried to find documentation on how to manually configure a RestController (ie in a Configuation class).我试图找到有关如何手动配置 RestController(即在 Configuation 类中)的文档。 That means without using the RestController annotation.这意味着不使用 RestController 注释。 Considering all the other annotations, like mapping, pathvariables etc. is at all possible?考虑所有其他注释,如映射、路径变量等,是否完全有可能?

A controller is essentially a component with a request mapping. controller 本质上是一个带有请求映射的组件。 See RequestMappingHandlerMapping .请参阅RequestMappingHandlerMapping

    @Override
    protected boolean isHandler(Class<?> beanType) {
        return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
                AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
    }

If you want to instantiate a "rest controller" through configuration, you can probably do so via the following:如果您想通过配置实例化“休息控制器”,您可以通过以下方式进行:

@Configuration
public class MyConfiguration {
   @Bean
   public MyController() {
      return new MyController();
   }
}
@ResponseBody
public class MyController {
   @RequestMapping("/test")
   public String someEndpoint() {
      return "some payload";
   }
}

But I don't think you'll be able to configure the request mappings (path variables, etc) in the configuration though;但是我认为您无法在配置中配置请求映射(路径变量等); at least I haven't seen an example nor figured out how.至少我还没有看到一个例子,也没有弄清楚如何。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM