java - 无法从 swagger UI 中的控制器获取我的端点列表
问题描述
我是使用 Swagger UI 的 Spring Boot 新手。我只是想将我的 Rest 控制器端点配置为在 swagger UI 屏幕上显示,但它显示 No operation for specs defined。很确定,这是一个配置问题。
我试过@EnableAutoConfiguration,还是找不到控制器
SwaggerDemoApplication.java
package com.example.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SwaggerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerDemoApplication.class, args);
}
}
SwaggerConfig.java
package com.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import static springfox.documentation.builders.PathSelectors.regex;
@EnableSwagger2
@Configuration
public class SwaggerConfig {
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
.paths(regex("/test.*"))
.build();
}
}
TestController.java
package com.example.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
@RestController
@RequestMapping(value = "/test")
@Api(value="onlinestore", description="Operations pertaining to products in Online Store")
public class TestController {
@RequestMapping(value = "/test-swagger", method= RequestMethod.GET)
public String home() {
return "Spring is here!";
}
}
预期:休息端点实际:规范中未定义操作
解决方案
我和你有完全相同的问题,这是由于我的@SpringBootApplication
班级安排错误造成的,就像你遇到的一样。
(在代码片段中,我故意省略了不太相关的注释,它们仍然必须存在,就像您在帖子中一样。
我还引用了“under”、“root package”等词,因为从技术上讲,Java 无法识别“子包”之类的东西。Java 中的所有包都处于同一“级别”,即使 Web 域的点和相似之处“误导”我们对它们进行分层思考。然而, Spring广泛适用于“子包”和“根包”。)
package com.example.config;
@SpringBootApplication
public class SwaggerDemoApplication {
...
}
package com.example.config;
@EnableSwagger2
public class SwaggerConfig {
...
.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
...
}
@RestController
package com.example.controller; // notice that this is not "under" com.example.config where SwaggerDemoApplication resides
public class TestController {
....
}
我观察到,如果@SpringBootApplication
该类不在该类的“根包”中@RestController
,它会破坏自动魔术行为,并且在调用中设置的包名将apis(...)
被忽略,并且不会扫描包。老实说,我不太确定应该如何apis()
工作以及它是错误还是功能。
要在不添加的情况下修复它@ComponentScan
,您的包组织应该是这样的:
package com.example.myapplication; // the main class is placed in the "root" package
@SpringBootApplication
public class SwaggerDemoApplication {
...
}
package com.example.myapplication.config;
@EnableSwagger2
public class SwaggerConfig {
...
.apis(RequestHandlerSelectors.basePackage("com.example.myapplication"))
...
}
package com.example.myapplication.controller;
@RestController
public class TestController {
...
}
您还可以通过指定进一步过滤 API 扫描
.apis(
withClassAnnotation(RestController.class)
.and(basePackage("com.example.myapplication.controller))
)
推荐阅读
- python - Error while running python with xammp server
- reactjs - 如何配置我的 React-Node 应用程序以便将其部署到 Heroku?
- react-native - 如何在本机反应中默认设置isConnected值false
- flutter - Cannot apply decoration to Container in Flutter
- reactjs - Returning API data with an input search
- javascript - Opening a document hyperlink present in one JQuery tab, in another JQuery tab?
- javascript - 读取文件对象javascript的内部值
- robotframework - 仅呈现元素时如何添加条件?
- gradle - 如何为示例 gRPC Java TLS 服务器/客户端创建启动脚本?
- sql - 如何修改 sql 查询以仅在出现次数大于 3 时才显示附加信息?