繁体   English   中英

Swagger 未显示任何控制器或端点

[英]Swagger is not showing any controllers or endpoints

Swagger Api 文档图像

我正在我的 springboot 项目中添加/集成 swagger。 我尝试了不同的东西,但它没有得到修复。 现在显示的只是没有任何端点或控制器的白页,只是一个带有招摇标志的空白页面。

招摇网址是:http://localhost:8080/swagger-ui.html

我的招摇配置如下:

package com.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication(scanBasePackages = {"com.app.controller"})
public class StoreApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        try {

            SpringApplication.run(StoreApplication.class, args);

        }catch (Throwable throwable){
            System.out.println(throwable.toString());
            throwable.printStackTrace();
        }
    }
}

这是我的控制器代码。

我的控制器

package com.app.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
public class CustomersController {

    @RequestMapping(value = "/customers", method = RequestMethod.GET)
    ResponseEntity<?> getAllCustomers(){


        return ResponseEntity.status(HttpStatus.OK).body(null);
    }

    @RequestMapping(value = "/customer", method = RequestMethod.POST)
    ResponseEntity<?> createCustomer(){


        return ResponseEntity.status(HttpStatus.OK).body(null);
    }

}

这是主要课程

主班

package com.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication(scanBasePackages = {"com.app.controller"})
public class StoreApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        try {

            SpringApplication.run(StoreApplication.class, args);

        }catch (Throwable throwable){
            System.out.println(throwable.toString());
            throwable.printStackTrace();
        }
    }
}

这是我的应用配置文件

应用配置

package com.app.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class AppConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry){

      //  registry.addRedirectViewController("/docApi/v2/api-docs","/v2/api-docs");
        registry.addViewController("/welcome").setViewName("Welcome");
    }


}

首先,尝试像这样添加招摇配置:

@EnableSwagger2
@Configuration
public class SwaggerConfig {


    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.app"))
                .paths(PathSelectors.any())
                .build();
    }

}

然后在控制器中添加一些注释,如下所示:

package com.app.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin
@RestController
@Api
public class CustomersController {

    @RequestMapping(value = "/customers", method = RequestMethod.GET)
    @ApiOperation(value = "get all", tags = "customer")
    ResponseEntity<?> getAllCustomers(){


        return ResponseEntity.status(HttpStatus.OK).body(null);
    }

    @RequestMapping(value = "/customer", method = RequestMethod.POST)
    @ApiOperation(value = "create", tags = "customer")
    ResponseEntity<?> createCustomer(){


        return ResponseEntity.status(HttpStatus.OK).body(null);
    }

}

希望这能解决您的问题。 然后尝试访问url:http: http://127.0.0.1:8080/swagger-ui/index.html :8080/swagger-ui/index.html,注意不是url http://localhost:8080/swagger-ui.html

暂无
暂无

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

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