Java openapi springboot服务器生成器是否有损坏的线程模型?
因此,我们尝试使用OpenAPI生成器,到目前为止,我们的结果好坏参半 复制步骤:Java openapi springboot服务器生成器是否有损坏的线程模型?,java,spring,spring-boot,spring-mvc,openapi-generator,Java,Spring,Spring Boot,Spring Mvc,Openapi Generator,因此,我们尝试使用OpenAPI生成器,到目前为止,我们的结果好坏参半 复制步骤: 下载openapi生成器jar:wgethttps://repo1.maven.org/maven2/org/openapitools/openapi-generator/4.0.3/openapi-generator-4.0.3.jar 为petstore生成springboot服务器示例:java-jar openapi-generator-cli-4.0.3.jar Generate-g spring-ih
wgethttps://repo1.maven.org/maven2/org/openapitools/openapi-generator/4.0.3/openapi-generator-4.0.3.jar
java-jar openapi-generator-cli-4.0.3.jar Generate-g spring-ihttps://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml
package org.openapitools.api;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.NativeWebRequest;
import java.util.Optional;
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2019-08-06T15:08:49.070+01:00[Europe/London]")
@Controller
@RequestMapping("${openapi.swaggerPetstore.base-path:/v1}")
public class PetsApiController implements PetsApi {
private final NativeWebRequest request;
@org.springframework.beans.factory.annotation.Autowired
public PetsApiController(NativeWebRequest request) {
this.request = request;
}
@Override
public Optional<NativeWebRequest> getRequest() {
return Optional.ofNullable(request);
}
}
招摇过市的代码生成器是出了名的糟糕,我的朋友说生成器有广度,但没有深度。您可以为各种语言和框架生成框架,但它们有严重的限制。例如,尝试使用
Page
或其他泛型从一个招摇过市的文档生成一个好的框架。我很遗憾地说,它们几乎没有任何实用性,而且这些工具往往只能以另一种方式可靠地工作,即先编码,然后生成SwaggerDoc
我工作过的一个地方有一个很好的概念,我非常喜欢这个概念,在实现它之前,您可以先设计API,这听起来像是您正在尝试的。一些IDE甚至支持生成的代码,并且有一些插件用于构建工具,如maven gradle等,以从yaml生成代码
但在实践中,我花了几天的时间试图从这些工具中获得理想的结果,最终放弃了。我认为真正的问题在于,Swagger/OpenAPI仍然被视为文档工具,而不是设计工具。我还认为,试图创建一个包罗万象的项目生成器从一开始就被设置为失败
我自己试图定制生成器使用的小胡子模板,但Java中的泛型是一场噩梦,你无法获得正确的工作流程,我将更改SwaggerDoc,然后更新代码,因为我的方法是生成一个接口,然后实现该接口,但是注释不是继承的,所以我必须复制所有的代码,这意味着没有任何好处。我不得不说这也是我的经验。我正试图加强我们的API文档实践(对whit说:我们应该有一些),但我必须承认我有点挣扎。任何输入或引用都是有用的。不要使用任何生成器来创建项目。在web上拿出一个配置了SpringBoot和SpringFox的模板。您还可以使用
swagger diff
来比较swagger文件,并确保您没有对API进行破坏性更改。谢谢!我考虑过springfox,但我被过去的一个案例推迟了,这个案例只有OpenAPI3才真正支持:我们需要传递一个对象数组,以纠正需要在同一事务中完成的域操作。类似于一批多种类型的远程过程调用,或者如果您真的扩展它,您可以将其与SQL事务进行比较。这需要一个操作员。我认为解决这个问题的唯一方法是手动创建OpenAPI3联系人,或者可能是完全不同的协议。有什么想法吗?
/**
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech) (4.0.3).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package org.openapitools.api;
import org.openapitools.model.Error;
import org.openapitools.model.Pet;
import io.swagger.annotations.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2019-08-06T15:08:49.070+01:00[Europe/London]")
@Validated
@Api(value = "pets", description = "the pets API")
public interface PetsApi {
default Optional<NativeWebRequest> getRequest() {
return Optional.empty();
}
@ApiOperation(value = "Create a pet", nickname = "createPets", notes = "", tags={ "pets", })
@ApiResponses(value = {
@ApiResponse(code = 201, message = "Null response"),
@ApiResponse(code = 200, message = "unexpected error", response = Error.class) })
@RequestMapping(value = "/pets",
produces = { "application/json" },
method = RequestMethod.POST)
default ResponseEntity<Void> createPets() {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
@ApiOperation(value = "List all pets", nickname = "listPets", notes = "", response = Pet.class, responseContainer = "List", tags={ "pets", })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "A paged array of pets", response = Pet.class, responseContainer = "List"),
@ApiResponse(code = 200, message = "unexpected error", response = Error.class) })
@RequestMapping(value = "/pets",
produces = { "application/json" },
method = RequestMethod.GET)
default ResponseEntity<List<Pet>> listPets(@ApiParam(value = "How many items to return at one time (max 100)") @Valid @RequestParam(value = "limit", required = false) Integer limit) {
getRequest().ifPresent(request -> {
for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
ApiUtil.setExampleResponse(request, "application/json", "null");
break;
}
}
});
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
@ApiOperation(value = "Info for a specific pet", nickname = "showPetById", notes = "", response = Pet.class, tags={ "pets", })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Expected response to a valid request", response = Pet.class),
@ApiResponse(code = 200, message = "unexpected error", response = Error.class) })
@RequestMapping(value = "/pets/{petId}",
produces = { "application/json" },
method = RequestMethod.GET)
default ResponseEntity<Pet> showPetById(@ApiParam(value = "The id of the pet to retrieve",required=true) @PathVariable("petId") String petId) {
getRequest().ifPresent(request -> {
for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
ApiUtil.setExampleResponse(request, "application/json", "null");
break;
}
}
});
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
}
@org.springframework.beans.factory.annotation.Autowired
public PetsApiController(NativeWebRequest request) {
this.request = request;
}