标签: springfox

Swagger for SpringMVC-Springfox的替代品?

你们中的任何人都可以为基于Spring MVC的REST API实现Swagger提出其他建议吗?仅供参考,这不是Spring Boot。我已经尝试过Springfox,但是恕我直言,这是一辆越野车。

spring-mvc swagger springfox

6
推荐指数
1
解决办法
3039
查看次数

如何在Springfox Swagger提供的Swagger/v2/api-docs中启用CORS头?

我的项目中有以下文件:

@Configuration
@Order(Ordered.LOWEST_PRECEDENCE)
public class SwaggerConfig {

    @Bean
    public Docket apiSwagger2Documentation() { .... }
}
Run Code Online (Sandbox Code Playgroud)

在Application.java中有:

@SpringBootApplication
@ComponentScan(basePackages = { ... })
@EnableSwagger2
public class Application {
    ...
}
Run Code Online (Sandbox Code Playgroud)

Swagger JSON可用/v2/api-docs,工作正常.

我想做的是为该端点启用CORS头.

对于我自己的控制器,我已经添加@CrossOrigin到控制器类,那些API然后有CORS头,工作正常.但是对于Swagger JSON URL我自己没有编写控制器,所以我不能使用那个注释.

我已经添加了以下方法SwaggerConfig,如Spring FrameworkCORS支持中的 "全局CORS配置"中所述.

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        System.out.println("*** corsConfigurer called");
        return new WebMvcConfigurerAdapter() {
            @Override public void addCorsMappings(CorsRegistry registry) {
                System.out.println("*** addCorsMappings called");
                registry.addMapping("/v2/api-docs");
            }
        };
    }
Run Code Online (Sandbox Code Playgroud)

打印两个打印语句,因此调用该方法.但是当我用curl调用URL时:

curl -H "Origin: foo.com"  \
   -H "Access-Control-Request-Method: …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-boot springfox

6
推荐指数
1
解决办法
1万
查看次数

无法使用Springfox发送授权承载令牌

我无法理解为什么使用Springfox 2.5.0在我的api中没有发送"Authorization:Bearer __".我有以下配置:

private ApiKey apiKey() {
        return new ApiKey(
                "Authorization", // name: My key - Authorization
                "api_key", // keyname: api_key
                "header");
    }

@Bean
    SecurityConfiguration security() {
        return new SecurityConfiguration(
                null, null, null,
                "Docserver2_fwk", // app name
                "BEARER", // api key value
                ApiKeyVehicle.HEADER, "Authorization", ",");
    }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 并且发送的卷曲是:

在此输入图像描述

看来我无法在springfox(2.5.0)中发送"Authorization:Bearer Token",这可能吗?,这是一个已知的问题吗?

类似的问题:https://github.com/springfox/springfox/issues/1812

PS:OpenAPI 3.0允许"承载"格式,例如:https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#jwt-bearer-sample

谢谢.

java spring swagger swagger-ui springfox

6
推荐指数
4
解决办法
1万
查看次数

swagger注释内容类型未设置

我有这个弹簧控制器:

@RestController
@RequestMapping("/communications")
class CommunicationController(private val service: CommunicationService) {

    @ApiOperation(
        produces = APPLICATION_JSON_VALUE, 
        consumes = APPLICATION_JSON_VALUE
    )
    @GetMapping(
        consumes = [APPLICATION_JSON_VALUE], 
        produces = [APPLICATION_JSON_VALUE]
    )
    fun findAll(
        criterias: CommunicationCriterias, 
        page: Pageable
    ): List<CommunicationDTO> = service.findCommunications(criterias, page)

}
Run Code Online (Sandbox Code Playgroud)

当我通过swagger-ui(springfox)界面测试此端点时,出现415: content type invalid错误.似乎content-type: application/json没有在标题中设置.

缺什么 ?

spring kotlin swagger-ui spring-boot springfox

6
推荐指数
1
解决办法
496
查看次数

Maven:包io.swagger.annotations不存在

我想大方地记录我的项目。我在项目中添加了大张旗鼓的批注和io.springfox依赖项,但运行时mvn clean package出现很多错误:

PS D:\parent-project> mvn clean package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] parent-project                              [pom]
[INFO] module-common-lib                           [jar]
[INFO] module1                                     [jar]
[INFO] module2                                     [jar]
[INFO]
[INFO] ------------< parent-project:parent-project >-------------
[INFO] Building parent-project 0.0.1-SNAPSHOT                      [1/4]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ parent-project ---
[INFO]
[INFO] -------< parent-project:module-common-lib >-------
[INFO] Building module-common-lib 0.0.1-SNAPSHOT           [2/4]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ module-common-lib ---
[INFO]
[INFO] --- …
Run Code Online (Sandbox Code Playgroud)

java spring-annotations maven swagger-2.0 springfox

6
推荐指数
1
解决办法
6492
查看次数

Swagger 未检测到使用 Spring Data Rest 构建的 Api

我正在开发一个 Spring Boot 应用程序,使用 swagger 为我的 API 生成文档,我正在使用 Spring data rest 生成 Api,但是当我运行该应用程序时,我收到 swagger 消息:规范中没有定义操作!

这是我的 API 代码:

@Api(tags = "projets")
@RepositoryRestResource(collectionResourceRel = "projets", path = "projets")
public interface IProjectRepository extends JpaRepository<Project, Long> {

}
Run Code Online (Sandbox Code Playgroud)

这是我的配置文件:

@Configuration
@EnableSwagger2
public class QfactoryConfiguration {

    @Bean
     public Docket getDocketInstance() {
         return new Docket(DocumentationType.SWAGGER_2) 
                 .apiInfo(new ApiInfoBuilder()
                            .title("Spring Boot project")
                            .description("Spring Boot bootstrap project")
                            .version("0.1")
                            .license("Unlicense")
                            .build())
                  .select()  
                  .apis(RequestHandlerSelectors.basePackage("com.errabi.qfactory.repositories"))             
                  .paths(PathSelectors.any())                          
                  .build();  
     }




}
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的依赖项:

<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-data-rest</artifactId>
        <version>2.9.2</version>
    </dependency> …
Run Code Online (Sandbox Code Playgroud)

java swagger spring-data-rest spring-boot springfox

6
推荐指数
1
解决办法
1万
查看次数

在Swagger 2中默认情况下是否可以处于试用模式?

我将Swagger 2与Springfox一起使用。

单击端点时,有任何方法可以默认激活“试用”模式,而无需单击此按钮:

在此处输入图片说明

swagger swagger-ui swagger-2.0 springfox

6
推荐指数
1
解决办法
69
查看次数

试图访问 swagger-ui 或 api/api-docs 但出现 404 错误

这是我的第一篇文章,所以请随时留下一些反馈,或者如果我做错了什么:)

我正在使用 spring-boot 和 resteasy :

<!-- Spring Boot -->
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-dependencies</artifactId>
 <version>2.1.0.RELEASE</version>
 <type>pom</type>
 <scope>import</scope>
</dependency>
<dependency>
  <groupId>com.paypal.springboot</groupId>
  <artifactId>resteasy-spring-boot-starter</artifactId>
  <version>2.3.4-RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用 Swagger 来查看我的端点,所以我添加了这个依赖项:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

此依赖项已加载到我的存储库中,一切看起来都不错。


我添加了这个类来制作最简单的配置类:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我在调试模式中启动应用程序时,我输入了这个以前的 Bean。一切看起来都很好。


当我启动 Spring 应用程序时:

@SpringBootApplication
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class,RabbitAutoConfiguration.class})
public class SituationServicesApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(SituationServicesApplication.class, args);
    }
    @Override …
Run Code Online (Sandbox Code Playgroud)

java swagger-ui spring-boot swagger-2.0 springfox

6
推荐指数
1
解决办法
5593
查看次数

Swagger 如何启用按标签过滤不区分大小写

我正在使用通过 Springfox 2.9.2 实现的 Swagger-UI。

我通过在 UiConfiguration 中将“filter”设置为 true 来启用“Filter By tag”。

@Configuration
@EnableSwagger2
@Import({BeanValidatorPluginsConfiguration.class})
//https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api
public class SwaggerConfiguration {
    .
    .
    .

    @Bean
    public UiConfiguration uiConfig() {
    return UiConfigurationBuilder.builder()
            .defaultModelRendering(ModelRendering.MODEL)
            .defaultModelsExpandDepth(-1)
            .displayRequestDuration(true)
            .displayOperationId(false)
            .filter(true)
            .deepLinking(true).build();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是这个过滤器区分大小写。我需要在没有大小写条件的情况下进行搜索..

我怎样才能让这个标签过滤器不区分大小写..?

在此处输入图片说明

tags filter case-insensitive swagger springfox

6
推荐指数
0
解决办法
935
查看次数

无法解析指针:/definitions/Error-ModelName

我是 Swagger.io 的新手,也是 Spring Fox 的新手。我遇到的问题是,由于某种原因,一个对象没有正确引用其模型。

用户界面中的错误: 在此输入图像描述

错误是因为 JSON 中的结果如下:

"schema": {
"$ref": "#/definitions/Error-ModelName{namespace='online.staffmanager.backend.auth.model.dto', name='UserChangeSet'}"
}
Run Code Online (Sandbox Code Playgroud)

如果我将其更改为:

"schema": {
"$ref": "#/definitions/UserChangeSet"
}
Run Code Online (Sandbox Code Playgroud)

它确实有效。我不知道为什么注释会这样映射。

我的注释:

 @Operation(
            tags = "auth",
            summary = "Create a new User Account",
            responses = {
                    @ApiResponse(
                            responseCode = "200",
                            content = @Content(schema = @Schema(implementation = TokenInfo.class))),
                    @ApiResponse(
                            responseCode = "201",
                            content = @Content(schema = @Schema(implementation = UserChangeSet.class)))
            }
    )
Run Code Online (Sandbox Code Playgroud)

SpringFox配置:

@Configuration
@EnableSwagger2
@EnableWebMvc
public class SpringFoxConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any()) …
Run Code Online (Sandbox Code Playgroud)

swagger swagger-ui spring-boot springfox

6
推荐指数
1
解决办法
1万
查看次数