合同第一代OpenAPI

时间:2018-08-21 15:13:23

标签: open-liberty openapi-generator

我尝试使用简单的OpenAPI V3 API在具有契约优先范式的OpenLiberty上实现。

我使用以下插件生成OpenAPI代码:

<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>3.2.2-SNAPSHOT</version>

对于世代,我使用<generatorName>jaxrs-spec</generatorName>

作为<configOptions>,我使用<useSwaggerAnnotations>false</useSwaggerAnnotations>

在模型类旁边,还会生成以下接口:

@Path("/inventory")
public interface InventoryApi {

  @GET
  @Path("/systems/{hostname}")
  @Produces({ "text/plain", "application/json" })
  Response getPropertiesForHost(@PathParam("hostname") String hostname);

  @GET
  @Path("/systems")
  @Produces({ "application/json" })
  Response listContents();
}

我尝试像这样尽可能精简地使用我的实现:

@RequestScoped
@Path("/")
public class InventoryImpl implements InventoryApi {

   public Response getPropertiesForHost(String hostname) {
      ...
   }

   public Response listContents() {
      ...
   }    
}

我可以使用以下curl命令调用 curl -X GET "http://localhost:9080/properties-sample/systems" 可行!

但是我本来希望使用以下内容 curl -X GET "http://localhost:9080/properties-sample/inventory/systems" 但这是行不通的。 我必须将Impl中的@Path更改为@Path("/inventory"),因此它可以使用curl -X GET "http://localhost:9080/properties-sample/inventory/systems"

这是按设计工作还是与界面上的@Path注释无关?

别人是否还有另一种在OpenLiberty中使用合同优先范式的方法?

1 个答案:

答案 0 :(得分:2)

您正确使用了OpenAPI Tools插件。 Open Liberty支持MicroProfile OpenAPI,它允许您公开合同优先的OAS3文档,但出于生成目的,Open Liberty属于OSS OpenAPI Tools社区。

This link应该有助于指导您的界面,而不是根据JAX-RS批注实现类决策。特别是,在您的示例中,您将覆盖主要的@Path注释,因此会产生不同的行为。