控制器没有出现在 swagger-ui.html 中 [英] Controller does not appear in swagger-ui.html
问题描述
我将 Swagger 2 与非 spring-boot 一起使用,我也可以在 Tomcat 上部署我的应用程序并成功运行它.但是控制器没有出现在 swagger-ui.html
中,只显示带有绿色 swagger 标题的空白页面.我在这个问题上花了两天时间.你能给我一些建议吗?
@Controller 表示如下类:
@Api@控制器@RequestMapping("/用户")公共类用户控制器{protected Logger logger = LoggerFactory.getLogger(UserController.class);@自动连线私人用户服务用户服务;@RequestMapping("/showInfos")公共@ResponseBody 对象 showUserInfos(){logger.info("---------------showUserInfos--------");列表<用户信息>userInfos = userService.getUsers();返回用户信息;}
我的spring-mvc.xml配置如下:
<!@Controller 注入 bean --><context:component-scan base-package="com.roy.demo, version"/><!-- 启用 swgger ui --><mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/><mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/><!-- 包括招摇配置--><bean name="/applicationSwaggerConfig" class="com.roy.demo.config.ApplicationSwaggerConfig"/>
也是我的swagger配置类如下:
@EnableSwagger2公共类 ApplicationSwaggerConfig {私有静态最终记录器 LOGGER = Logger.getLogger(ApplicationSwaggerConfig.class);@豆角,扁豆公共 Docket api() {LOGGER.info("############################## 进入 Docket api() ####################################");返回新的 Docket(DocumentationType.SWAGGER_2).选择().apis(RequestHandlerSelectors.basePackage("com.roy.demo.controller")).paths(PathSelectors.any()).建造();}
}
我的 maven pom.xml swagger2 依赖如下:
<依赖><groupId>io.swagger</groupId><artifactId>swagger-core</artifactId><version>1.5.3</version></依赖><依赖><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.5.0</version></依赖><依赖><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.5.0</version></依赖>
Bellow 是我输入端点 url 时的结果:
我也是 Swagger 新手,但是下面的代码我用于我的 swagger 配置,它对我来说效果很好.我在课堂上完成了配置.
配置类.
@Configuration@EnableWebMvc@EnableSwagger2@ComponentScan(basePackages = "com.*")@PropertySource(value = { "classpath:log4j.properties" })公共类 SpringConfig 扩展了 WebMvcConfigurerAdapter {@豆角,扁豆公共 Docket api() {返回新 Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()).directModelSubstitute(LocalDate.class, String.class).genericModelSubstitutes(ResponseEntity.class).useDefaultResponseMessages(false).选择().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).建造();}@覆盖public void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}@SuppressWarnings("弃用")私有 ApiInfo apiInfo() {ApiInfo apiInfo = 新的 ApiInfo("API","xxxx 的 API","API TOS",服务条款","xxx",API许可证","");返回 apiInfo;}}
Maven 依赖:
<依赖><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.4.0</version></依赖><依赖><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.4.0</version></依赖>
控制器类
@RestController@Api(value="users", description="用户管理端点")公共类控制器{}
端点网址:
https://localhost:8080/AppName/swagger-ui.html
I use Swagger 2 with non-spring-boot and I also can deploy my app on Tomcat and run it successfully. But the controller does not appear in swagger-ui.html
, just the empty page with the green swagger title show up. I have spent two days on this issue. Would you give me some advice?
@Controller means the class as bellow:
@Api
@Controller
@RequestMapping("/user")
public class UserController {
protected Logger logger = LoggerFactory.getLogger(UserController.class);
@Autowired
private UserService userService;
@RequestMapping("/showInfos")
public @ResponseBody Object showUserInfos(){
logger.info("-----------------------showUserInfos-----------------------");
List<UserInfo> userInfos = userService.getUsers();
return userInfos;
}
my spring-mvc.xml configuration as follows:
<mvc:annotation-driven/>
<!@Controller inject bean -->
<context:component-scan base-package="com.roy.demo , version" />
<!-- Enables swgger ui -->
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
<!-- Include a swagger configuration -->
<bean name="/applicationSwaggerConfig" class="com.roy.demo.config.ApplicationSwaggerConfig" />
also my swagger configuration class is as follows:
@EnableSwagger2
public class ApplicationSwaggerConfig {
private static final Logger LOGGER = Logger.getLogger(ApplicationSwaggerConfig.class);
@Bean
public Docket api() {
LOGGER.info("################################ into Docket api() #####################################");
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.roy.demo.controller"))
.paths(PathSelectors.any())
.build();
}
}
my maven pom.xml swagger2 dependency as follows:
<!-- Swagger 2.0 -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-core</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
Bellow is the result when i enter the endpoint url:http://localhost:8080/Spring_SpringMVC_Mybatis/swagger-ui.html
I also new to Swagger but Below code I used for my swagger configuration and it works well for me.I done the configuration in class.
Configuration class.
@Configuration
@EnableWebMvc
@EnableSwagger2
@ComponentScan(basePackages = "com.*")
@PropertySource(value = { "classpath:log4j.properties" })
public class SpringConfig extends WebMvcConfigurerAdapter {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()).directModelSubstitute(LocalDate.class, String.class).genericModelSubstitutes(ResponseEntity.class)
.useDefaultResponseMessages(false)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@SuppressWarnings("deprecation")
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo(
"API",
"API for xxxx",
"API TOS",
"Terms of service",
"xxx",
"License of API",
"");
return apiInfo;
}
}
Maven Dependency:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
Controller Class
@RestController
@Api(value="users", description="Endpoint for user management")
public class Controller {
}
endpointurl:
https://localhost:8080/AppName/swagger-ui.html
这篇关于控制器没有出现在 swagger-ui.html 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!