我正在尝试使用OpenJDK 15、Spring Boot 2.6.0、Springfox 3 启动 Spring Boot 项目。
我们正在做一个项目,取代Netty作为 Web 服务器并使用 Jetty 来代替,因为我们不需要非阻塞环境。
在代码中我们主要依赖Reactor API(Flux、Mono),所以我们无法删除org.springframework.boot:spring-boot-starter-webflux依赖。
我在一个新项目中复制了我们遇到的问题:https://github.com/jvacaq/spring-fox。
我发现build.gradle文件中的这些行是问题的根源。
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
Run Code Online (Sandbox Code Playgroud)
这是build.gradle文件:
plugins {
id 'org.springframework.boot' version '2.6.0'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
implementation 'org.springframework.boot:spring-boot-starter-webflux'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
implementation "io.springfox:springfox-boot-starter:3.0.0" …Run Code Online (Sandbox Code Playgroud) 我有一个用简单的Spring编写的ReSTFul API(没有Spring Boot,没有花哨的东西!).我需要实现Swagger.到目前为止,互联网上的每一页都只让我感到疯狂,因为令人困惑的配置和臃肿的代码,我根本找不到便携式.
有没有人有一个示例项目(或一组详细步骤)可以帮助我实现这一目标?特别是,我正在寻找一个使用swagger-springmvc的好样本.我知道它有'样本',但充其量,深奥的代码令人沮丧.
我必须澄清一点,我不是在寻找"为什么Swagger是最好的".我没有使用(并且我目前的任务不会使用)Spring Boot等.
我正在尝试运行我的 Spring boot 应用程序,该应用程序基于带有 swagger UI 的 3.0 版本,并且遇到了很多异常,我已经探索了许多来源,例如 youtube 和文档,但我无法找到解决方案。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
如果有人可以分享解决方案,那就太好了。
关于在Spring MVC中集成Swagger:
Swagger没有显示GET/PUT/POST文档@RequestMapping
在我的Spring MVC Rest webservice应用程序中,我有一个Login控制器和一个Student Controller.我刚刚配置了Swagger来生成Rest API文档.参考:http://java.dzone.com/articles/how-configure-swagger-generate
问:然而,Swagger只显示类级路径,我猜它不是显示类级别的@RequestMapping.,方法级别映射被忽略.有什么理由吗?
@Controller
@RequestMapping(value = "/login")
public class LoginController {
@RestController
@RequestMapping(value = "/students/")
public class StudentController {
@RequestMapping(value = "{departmentID}", method = RequestMethod.GET)
public MyResult getStudents(@PathVariable String departmentID) {
// code
}
@RequestMapping(value = "student", method = RequestMethod.GET)
public MyResult getStudentInfo(
@RequestParam(value = "studentID") String studentID,
@RequestParam(value = "studentName") String studentName) {
//code
}
@RequestMapping(value = "student", method = RequestMethod.POST) …Run Code Online (Sandbox Code Playgroud) 这是我的代码:我从 application.properties 文件 SwaggerConfig.java 获取所有值
@Configuration
@EnableSwagger2
@Profile("!prod")
@PropertySource(value = { "classpath:application.properties" })
public class SwaggerConfig {
@Value("${swagger.api.title}")
private String title;
@Value("${swagger.api.description}")
private String description;
@Value("${swagger.api.termsOfServiceUrl}")
private String termsOfServiceUrl;
@Value("${swagger.api.version}")
private String version;
@Value("${swagger.api.controller.basepackage}")
private String basePackage;
@Bean
public Docket postMatchApi() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage(basePackage))
.paths(PathSelectors.ant("/**")).build().apiInfo(metaData());
}
private ApiInfo metaData() {
return new ApiInfoBuilder().title(title).description(description).termsOfServiceUrl(termsOfServiceUrl)
.version(version).build();
}
Run Code Online (Sandbox Code Playgroud)
这是我的 springboot 初始化程序:
@SpringBootApplication
@ComponentScan(basePackages = { "com.example.demo" })
@ComponentScan(basePackageClasses = {AppInitializer.class, SwaggerConfig.class})
@EnableAsync
@EnableRetry
public class AppInitializer{
public static void main(String[] args) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的Spring Boot版本2.0.1.RELEASE与Swagger集成 .
从这篇博文中 看来,通过添加两个Maven依赖项似乎很容易,一切都应该有效.
所以我在pom中添加了以下依赖项:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
并创建了SwaggerConfigbean:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
return docket;
}
}
Run Code Online (Sandbox Code Playgroud)
在属性文件中,我在尝试使其工作期间最终得到了这3个条目:
spring.application.name=cat-service
management.server.servlet.context-path=/cat-service
server.servlet.contextPath=/cat-service
Run Code Online (Sandbox Code Playgroud)
但最后,访问时
http://localhost:8080/cat-service/api/v2/api-docs
或UI页面
http://localhost:8080/cat-service/swagger-ui.html
我收到一个page not found错误.
我在swagger github页面 和stackoverflow 中的这个问题中发现了这个问题 但是我无法改变我的404错误.
我正在尝试使用Spring启动应用程序配置Swagger UI.虽然v2/api-docs似乎正在加载正确,但是http://localhost:8080/swagger-ui.html不会加载我带注释的REST API.
这是我有的:
pom.xml中:
...
<!--Swagger UI-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
...
Run Code Online (Sandbox Code Playgroud)
SwaggerConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
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;
@Configuration
@EnableSwagger2
public class SwaggerConfig
{
@Bean
public Docket api()
{
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(regex("/.*"))
.build().apiInfo(apiInfo());
}
private ApiInfo apiInfo()
{
ApiInfo apiInfo = new ApiInfo(
"My Project's REST API",
"This is a description of your API.", …Run Code Online (Sandbox Code Playgroud) 这是我的pom.xml:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我正在使用1.5.3.RELEASESpring Boot 的版本。这是我的招摇配置文件:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket swagger() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的WebSecurityConfig.java:
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources", "/configuration/security", "/swagger-ui.html", "/webjars/**");
}
Run Code Online (Sandbox Code Playgroud)
当我从端点http://localhost:8080/v2/api-docs获取时,我会返回我的 JSON:
{
"swagger": "2.0",
"info": {
"description": "Api Documentation",
"version": "1.0",
"title": "Api Documentation",
"termsOfService": "urn:tos",
"contact": {},
"license": {
"name": …Run Code Online (Sandbox Code Playgroud) java ×5
spring ×5
spring-boot ×5
swagger ×5
gradle ×2
spring-mvc ×2
springfox ×2
java-11 ×1
swagger-2.0 ×1
swagger-ui ×1