当前位置: 首页 > 知识库问答 >
问题:

如何使用Swagger2生成Spring-boot REST API文档?

傅明知
2023-03-14

我试图使用Swagger2生成Spring-boot应用程序的REST API文档。

下面是我的application.properties文件内容:

server.port = ${port:8082}
server.contextPath=/myServicePath
spring.h2.console.enabled=true
logging.level.org.hibernate.SQL=debug
spring.datasource.url=jdbc:mysql://${mysql-host:localhost}:${mysql-port:3306}/${mysql-dbname:mydb}
spring.datasource.username=${mysql-user:root}
spring.datasource.password=${mysql-password:password}
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import static springfox.documentation.builders.PathSelectors.*;
@Configuration
public class ApiDocumentationConfiguration {
    @Bean
    public Docket documentation() {
    return new Docket(DocumentationType.SWAGGER_2)
      .select()
        .apis(RequestHandlerSelectors.any())
        //.paths(regex("/.*"))
        .build()
      .pathMapping("/")
      .apiInfo(metadata());
    }
    @Bean
    public UiConfiguration uiConfig() {
      return UiConfiguration.DEFAULT;
    }
    private ApiInfo metadata() {
      return new ApiInfoBuilder()
    .title("My awesome API")
    .description("Some description")
    .version("1.0")
    .contact("my-email@domain.org")
    .build();
    }
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@ComponentScan
@EnableSwagger2
public class Application {
    public static void main( String[] args ) 
    {
        SpringApplication.run(Application.class, args);
    }
}
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RestController

public class MyController {

    @Autowired
    private HttpServletRequest request;

    @ApiOperation(value = "doStuff", nickname = "doStuff", response = Response.class)
    @RequestMapping(method = RequestMethod.GET, produces = "application/json")
    public String doStuff(@RequestBody String command) {
        return "TestString";
    }       
}
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.1.1</version>            
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.1.1</version>
</dependency>

有人能帮我弄清楚这件事吗?

提前谢谢你。

共有1个答案

公西飞鸾
2023-03-14

您可能需要一个资源处理程序。可能是这样的:

@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/");
}
 类似资料:
  • 我正在使用NSwag并尝试将OpenAPI JSON文档转换为版本2。这是我的配置: 但是,当我将生成的OpenAPI文件粘贴到Swagger Editor中时,它会显示错误: 如何配置NSwag以生成正确的OpenAPI 2.0文件?

  • 我试图为符合Java Card 2.2.1平台的智能卡生成以下程序的CAP文件: 因此,我将其保存在名为的文件中,然后将其编译为文件,如下所示: 问题1:这个警告是为了什么? 之后,我尝试将这个文件转换为其形式: 为什么转换器要寻找这个路径?我指定目录作为类目录,但它将我指定的路径与我的程序包名连接起来!为什么? Q3:当我们使用Winrar打开文件时,我们可以在header.cap和Applet

  • 我使用的是Spring Batch 2。版本我已经生成了csv文件,并能够在本地以csv格式保存。 现在我想生成相同的文件,但它将存储在SFTP服务器上。 我已经阅读了一些在sftp服务器上生成文件的教程,但它们使用的是spring与spring Batch的集成。 是否可以仅使用Spring批处理在SFTP服务器上生成文件? 下面是itemReader bean defined:: ItemWr

  • 我正在使用gradle创建一个spring boot应用程序。我需要创建一个jar,其中包含在(aws)ec2实例上部署所需的所有库。 我正试图创建一个罐子由。。。 1)点击项目结构 2)然后点击工件 3)点击- 4) 用默认设置点击ok 5) 点击build标签 6)我有一个jar文件,但它不包含类。

  • 我正在用Spring Boot、Kotlin和Thymeleaf构建一个web应用程序。我有一些HTML模板,但我想让其中一个返回XML文件。这个XML将是一个使用ThymleLab属性的ThymleLab模板。在Spring Boot中,正确的方法是什么?此外,还应下载XML。 我看过这个:Spring靴

  • 我如何加密一个密码,把它插入db和比较后,他什么时候会想要连接? 我将使用StandardPasswordEncoder Spring security 3.1.4加密我的密码并将其插入DB。但是我如何回收方法产生的盐呢? 我问她是因为我需要selt命令来重新编码比较的密码?并验证用户是否必须输入正确的密码? 这里的密码编码:和我会比较编码新密码 链接文档Spring:http://docs.Sp