Java Springdoc随机api文档生成

Java Springdoc随机api文档生成,java,springdoc,Java,Springdoc,我希望生成一个api,采取不同的内容类型 我面临的问题是,如果我多次运行我的应用程序,我会有不同的输出文档 @RestController public class MyRestController { @Operation(summary = "GetMyData", operationId = "gettt", responses = @ApiResponse(responseCode = "204", c

我希望生成一个api,采取不同的内容类型

我面临的问题是,如果我多次运行我的应用程序,我会有不同的输出文档

@RestController
public class MyRestController {

    @Operation(summary = "GetMyData", operationId = "gettt",
        responses = @ApiResponse(responseCode = "204", content = @Content(mediaType = "application/vnd.something")))
    @GetMapping(produces = "application/vnd.something")
    public ResponseEntity<Void> getSomethingElse() {
        return noContent().build();
    }

    @GetMapping(produces = TEXT_PLAIN_VALUE)
    public String get() {
        return "some text";
    }

    @GetMapping(produces = HAL_JSON_VALUE)
    public EntityModel<JsonResponse> getHal() {
        return EntityModel.of(new JsonResponse(),
            linkTo(MyRestController.class).slash("somelink").withSelfRel()
        );
    }

    @GetMapping(produces = APPLICATION_JSON_VALUE)
    public JsonResponse getJson() {
        return new JsonResponse();
    }
}
如果在不更改代码的情况下重新启动服务器,将生成以下响应

"operationId": "gettt_1",
"responses": {
    "200": {
        "content": {
            "application/hal+json": {
                "schema": {
                    "$ref": "#/components/schemas/EntityModelJsonResponse"
                }
            },
            "application/json": {
                "schema": {
                    "$ref": "#/components/schemas/JsonResponse"
                }
            },
            "text/plain": {
                "schema": {
                    "type": "string"
                }
            }
        },
        "description": "OK"
    },
    "204": {
        "content": {
            "application/vnd.something": {}
        },
        "description": "No Content"
    }
},


我希望重新启动服务器将始终生成相同的文档

您看过文档了吗

@RestController
public class MyRestController {

    @Operation(summary = "GetMyData", operationId = "gettt",
        responses = @ApiResponse(responseCode = "204", content = @Content(mediaType = "application/vnd.something")))
    @GetMapping(produces = "application/vnd.something")
    public ResponseEntity<Void> getSomethingElse() {
        return noContent().build();
    }

    @GetMapping(produces = TEXT_PLAIN_VALUE)
    public String get() {
        return "some text";
    }

    @GetMapping(produces = HAL_JSON_VALUE)
    public EntityModel<JsonResponse> getHal() {
        return EntityModel.of(new JsonResponse(),
            linkTo(MyRestController.class).slash("somelink").withSelfRel()
        );
    }

    @GetMapping(produces = APPLICATION_JSON_VALUE)
    public JsonResponse getJson() {
        return new JsonResponse();
    }
}
您可以使用swagger ui属性,而无需覆盖标准的排序方式(operationsSorter和tagsSorter)

例如:

springdoc.swagger-ui.operationsSorter=method
springdoc.swagger-ui.tagsSorter=alpha
如果您希望在服务器端进行排序,可以使用OpenApiCustomiser对元素进行排序

这是一个示例代码,您可以使用比较器进行自定义,具体取决于所需的排序逻辑:

例如,对于架构的字母顺序排序:

@Bean
公共OpenApiCustomiser Sortschemalaphabetically(){
返回openApi->{
Map schemas=openApi.getComponents().getSchemas();
openApi.getComponents().setSchemas(新的树映射(模式));
};
}
按字母顺序对标记进行排序的示例:

@Bean
public OpenApiCustomiser sortTagsAlphabetically() {
    return openApi -> openApi.setTags(openApi.getTags()
            .stream()
            .sorted(Comparator.comparing(tag -> StringUtils.stripAccents(tag.getName())))
            .collect(Collectors.toList()));
}

您可以完全控制元素顺序,并且可以根据您的用例对它们进行排序…

我将编辑我的问题,使其更加具体,但我的问题是,我的类中方法的顺序会更改文档的“形状”(不仅是顺序),某些部分会丢失。