java - OpenAPI/Swagger-UI注释和@BeanParam

标签 java jax-rs swagger openapi quarkus

我正在使用Quarkus开发一组JAX-RS服务。我还使用OpenAPI / Swagger-UI注释对它们进行注释,以便于生成API文档。我可以像这样注释我的GET服务...

    @Path("/{communityIdentifier}")
    @GET
    @Operation(summary = "Get community details", description = "Get detailed information about a community")
    @APIResponses({
            @APIResponse(responseCode = "200", description = "The community details", content = @Content(schema = @Schema(ref = "community"))),
            @APIResponse(name = "401", responseCode = "401", description = "Authentication required"),
            @APIResponse(name = "403", responseCode = "403", description = "Permission denied - you do not have access to access this resource", content = @Content(schema = @Schema(ref = "baseError"))),
            @APIResponse(name = "404", responseCode = "404", description = "Resource not found", content = @Content(schema = @Schema(ref = "baseError"))),
            @APIResponse(name = "500", responseCode = "500", description = "Internal service error", content = @Content(schema = @Schema(ref = "baseError"))) })
    @Timed(name = "getCommunityTimer")
    @Counted(name = "getCommunityCount")
    public Response getCommunity(
            @PathParam("securityRealm") @Parameter(name = "securityRealm", description = "The security realm name", required = true) String securityRealmName,
            @PathParam("communityIdentifier") @Parameter(name = "communityIdentifier", description = "The community identifier", required = true) String communityIdentifier) {
 // Stuff
}


当我访问Swagger UI端点时,我看到了该服务的详细记录条目,包括有关securityRealmcommunityIdentifier参数的信息。现在,我试图以类似方式创建POSTPUT方法,并且遇到了问题。

由于我的PUT / POST请求包含许多表单参数,因此我将它们封装到一个对象中,并使用@BeanParam注释该方法。我的表单对象如下所示:

public class CommunityRequestForm extends AbstractRequestForm {

    private static final long serialVersionUID = 6007695645505404656L;

    @FormParam("description")
    private String description = null;

    @FormParam("name")
    private String name = null;

    public String getDescription() {
        return description;
    }

    public String getName() {
        return name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setName(String name) {
        this.name = name;
    }

}


我的POST方法如下所示:

    @Path("/")
    @POST
    @Consumes({ MediaType.APPLICATION_FORM_URLENCODED })
    @Operation(summary = "Create a community", description = "Creates a new community within a security realm")
    @APIResponses({
            @APIResponse(responseCode = "201", description = "The community details", content = @Content(schema = @Schema(ref = "community"))),
            @APIResponse(name = "401", responseCode = "401", description = "Authentication required"),
            @APIResponse(name = "403", responseCode = "403", description = "Permission denied - you do not have access to access this resource", content = @Content(schema = @Schema(ref = "baseError"))),
            @APIResponse(name = "404", responseCode = "404", description = "Resource not found", content = @Content(schema = @Schema(ref = "baseError"))),
            @APIResponse(name = "500", responseCode = "500", description = "Internal service error", content = @Content(schema = @Schema(ref = "baseError"))) })
    public Response createCommunity(
            @PathParam("securityRealm") @Parameter(name = "securityRealm", description = "The security realm name", required = true) String securityRealmName,
            @BeanParam CommunityRequestForm communityForm) {
  // Stuff
}


到目前为止,一切都很好。但是,我还无法弄清楚如何注释namedescription参数,因此它们由OpenAPI / Swagger-UI记录。我尝试将参数注释和定义更改为如下所示:

@FormParam("description")
@Parameter(name = "description", required = true, style = ParameterStyle.FORM)
private String description = null;


那什么也没做。我尝试将方法签名更改为如下形式:

public Response createCommunity(
            @PathParam("securityRealm") @Parameter(name = "securityRealm", description = "The security realm name", required = true) String securityRealmName,
            @Parameter(style = ParameterStyle.FORM) @BeanParam CommunityRequestForm communityForm)


依然没有。我的问题是,有没有一种方法可以使OpenAPI / Swagger-UI批注很好地播放并记录带有@BeanParam注释的对象?还是根本不支持这种方法?

最佳答案

我知道他们通过此PR:https://github.com/smallrye/smallrye-open-api/pull/138改进了SmallRye OpenAPI 1.1.5中的所有功能。

Quarkus 0.22.0仍使用1.1.3。我们已经更新了master中的1.1.8,因此即将发布的0.23.0将进行更新。

您可以尝试使用Quarkus master分支(只需使用mvn clean install -DskipTests -DskipITs构建它,然后将版本更改为999-SNAPSHOT)。希望情况会更好。

关于java - OpenAPI/Swagger-UI注释和@BeanParam,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57998793/

相关文章:

python-3.x - Swagger-UI 生成的 python 服务器由于 'no module named' 错误而未启动

java - libGDX:根据用户的输入更改 PNG 的颜色

jax-rs - 如何在 Quarkus 中处理大文件上传

java - 存储引用和对象之间的区别?

java - 返回接口(interface)实例数组

java - 如何正确配置jax-rs web service web.xml

python - 以编程方式从 Swagger 定义生成客户端

Swagger-Web Api 文档(Swashbuckle 中缺少 Bootstrapper)

java - 使用 GSON 避免在 json 映射中使用原始数据类型默认值

java - 当有两个以上线程运行时,如何使两个线程仅相互响应 - Java