代码之家  ›  专栏  ›  技术社区  ›  tomaytotomato

Spring引导控制器终结点未启用?

  •  0
  • tomaytotomato  · 技术社区  · 7 年前

    我继承了一个springboot微服务,它没有服务或API层,它的行为是HATEOAS风格的。

    这不是一个最佳的架构,需要改成MVC。

    目前,所有的存储库方法都是使用 @RepositoryRestResource 注释。

    还要注意的是,在调试控制器端点时,它实际上并没有到达。它被绕过了,这是另一个线索。

    @CrossOrigin
    @RestController
    @RequestMapping("/fixing")
    public class FixingController {
    
      private final FixingRepository fixingRepository;
    
      @Autowired
      FixingController(final FixingRepository fixingRepository) {
        this.fixingRepository = checkNotNull(fixingRepository, "Fixing Repository cannot be null");
      }
    
      /**
       * Builds a list of Fixing strings from the database
       * @return list
       */
      @RequestMapping(value = "/", method = RequestMethod.GET)
      public List<String> getAllFixings() {
    
        final List<String> fixingList = new ArrayList<>();
        for (Fixing fixing : fixingRepository.findAll()) {
          String name = fixing.getName();
          fixingList.add(name);
        }
        return fixingList;
      }
    
    }
    

    这是弹簧大摇大摆的配置

    @Configuration
    public class SwaggerConfig {
    
    @Bean
    public Docket api() {
    
    return new Docket(DocumentationType.SWAGGER_2)
      .select()
      .apis(RequestHandlerSelectors.any())
      .paths(PathSelectors.regex("/api.*"))
      .build();
     } 
    }
    

    存储库(注 注释)

    public interface FixingRepository extends JpaRepository<Fixing, Long> {
    
      @Override
      Fixing findOne(Long id);
    
      @Override
      List<Fixing> findAll();
    
    }
    

    当我重建并启动服务时,没有显示控制器。它只显示所有实体及其存储库方法。

    enter image description here

    POM相关性

     <dependencies>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-rest</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
            <!--  included explicitly to avoid javadoc generation error
                  due to a conflict with a class used by @Transactional annotation -->
            <dependency>
                <groupId>javax.interceptor</groupId>
                <artifactId>javax.interceptor-api</artifactId>
                <version>1.2</version>
            </dependency>
    
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
                <version>8.0.12</version>
            </dependency>
    
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.8.0</version>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
    
    
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.8.0</version>
            </dependency>
    
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-data-rest</artifactId>
                <version>2.8.0</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <version>1.5.13.RELEASE</version>
                <scope>test</scope>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.json/json -->
            <dependency>
                <groupId>org.json</groupId>
                <artifactId>json</artifactId>
                <version>20180130</version>
            </dependency>
    
            <dependency>
                <groupId>com.vladmihalcea</groupId>
                <artifactId>hibernate-types-52</artifactId>
                <version>2.2.2</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.5</version>
            </dependency>
    
            <dependency>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-core</artifactId>
                <version>3.6.2</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-gcp-starter-storage</artifactId>
                <version>1.0.0.RELEASE</version>
            </dependency>
    
        </dependencies>
    

    知道是什么原因吗?在配置中我看不到任何其他东西阻止它工作

    1 回复  |  直到 7 年前
        1
  •  2
  •   Arun Patra    7 年前

    问题在于你的傲慢。您只能通过以下方式选择API的一个子集(源于JPA存储库或源于RestController):

    .paths(PathSelectors.regex("/api.*"))
    

    我复制了您的场景,并注释了路径选择,我可以看到这两种类型的api。请注意,还可以使用自定义谓词来选择路径:

    @Configuration
    @Import({SpringDataRestConfiguration.class})
    public class SwaggerConfig {
    
        @Autowired
        @SuppressWarnings({"UnusedDeclaration"})
        private ServletContext servletContext;
    
        @Bean
        public Docket api() {
    
            return new Docket(DocumentationType.SWAGGER_2)
                    .pathProvider(relativePath())
                    .select()
                    .apis(RequestHandlerSelectors.any())
    //                .paths(paths2())
                    .build();
        }
        // Select only a few
        private Predicate<String> paths2() {
            return and(
                    (regex("/fixing.*")),
                    (regex("/api.*")));
        }
        // Exclude these 
        private Predicate<String> paths() {
            return and(
                    not(regex("/error.*")),
                    not(regex("/metrics.*")),
                    not(regex("/jolokia.*")),
                    not(regex("/health.*")),
                    not(regex("/env.*")),
                    not(regex("/metrics.*")),
                    not(regex("/info.*")),
                    not(regex("/mappings.*")),
                    not(regex("/trace.*")),
                    not(regex("/dump.*")),
                    not(regex("/heapdump.*")),
                    not(regex("/configprops.*")),
                    not(regex("/beans.*")),
                    not(regex("/autoconfig.*")),
                    not(regex("/logfile.*")),
                    not(regex("/shutdown.*")),
                    not(regex("/actuator.*")));
        }
    }
    

    @CrossOrigin
    @RestController
    @RequestMapping("/fixing")
    public class FixingController {
    
        /**
         * Builds a list of Fixing strings from the database
         * @return list
         */
        @RequestMapping(value = "/", method = RequestMethod.GET)
        public List<String> getAllFixingsViaRestController() {
    
            final List<String> fixingList = new ArrayList<>();
            fixingList.add("foo");
            fixingList.add("bar");
    
            return fixingList;
        }
    
    }
    

    现在我的大摇大摆的用户界面是这样的;您可以看到JPA存储库贡献的REST API和RestController贡献的API(/fixing path):

    enter image description here