Spring Boot Controller端点未启用?

时间:2018-11-02 11:06:21

标签: java spring-boot swagger

我继承了一个Spring Boot微服务,该服务没有Service或API层,它表现为HATEOAS风格。

这不是最佳架构,需要将其更改为MVC。

当前,所有存储库方法都可以使用@RepositoryRestResource批注直接访问。

计划是对其进行重构,并添加控制器和API层(DTO),但是在添加控制器后,摇摇晃晃不显示Rest控制器 还要注意,在调试控制器端点时,实际上并没有达到。它被绕开了,这是另一个线索。

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

存储库(注意没有@RepositoryRestResource注释)

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 个答案:

答案 0 :(得分:2)

问题出在您的SwaggerConfig上。您只能通过以下方式选择您的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;
    }

}

现在我的Swagger UI看起来像这样;您可以看到JPA存储库提供的REST API和RestController提供的API(/修复路径):

enter image description here