代碼優先還是設計優先?說說API開發技術(七)
openapi-generator generate -i ./petstore.yaml -g spring -o out/server/springboot
UserController.java
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 = "2020-04-30T14:18:18.784-07:00[America/Los_Angeles]") @Controller @RequestMapping("${openapi.openAPIPetstore.base-path:/v2}") public class UserApiController implements UserApi { private final NativeWebRequest request; @org.springframework.beans.factory.annotation.Autowired public UserApiController(NativeWebRequest request) { this.request = request; } @Override public Optional
UserApi.java
/** * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech) (4.3.0). * https://openapi-generator.tech * Do not edit the class manually. */ package org.openapitools.api; import java.util.List; import org.openapitools.model.User; 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.CookieValue; 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 = "2020-04-30T14:18:18.784-07:00[America/Los_Angeles]") @Validated @Api(value = "user", description = "the user API") public interface UserApi { default Optional
/** * POST /user/createWithArray : Creates list of users with given input array * * @param body List of user object (required) * @return successful operation (status code 200) */ @ApiOperation(value = "Creates list of users with given input array", nickname = "createUsersWithArrayInput", notes = "", tags={ "user", }) @ApiResponses(value = { @ApiResponse(code = 200, message = "successful operation") }) @RequestMapping(value = "/user/createWithArray", method = RequestMethod.POST) default ResponseEntity
/** * POST /user/createWithList : Creates list of users with given input array * * @param body List of user object (required) * @return successful operation (status code 200) */ @ApiOperation(value = "Creates list of users with given input array", nickname = "createUsersWithListInput", notes = "", tags={ "user", }) @ApiResponses(value = { @ApiResponse(code = 200, message = "successful operation") }) @RequestMapping(value = "/user/createWithList", method = RequestMethod.POST) default ResponseEntity
/** * DELETE /user/{username} : Delete user * This can only be done by the logged in user. * * @param username The name that needs to be deleted (required) * @return Invalid username supplied (status code 400) * or User not found (status code 404) */ @ApiOperation(value = "Delete user", nickname = "deleteUser", notes = "This can only be done by the logged in user.", tags={ "user", }) @ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid username supplied"), @ApiResponse(code = 404, message = "User not found") }) @RequestMapping(value = "/user/{username}", method = RequestMethod.DELETE) default ResponseEntity
/** * GET /user/{username} : Get user by user name * * @param username The name that needs to be fetched. Use user1 for testing. (required) * @return successful operation (status code 200) * or Invalid username supplied (status code 400) * or User not found (status code 404) */ @ApiOperation(value = "Get user by user name", nickname = "getUserByName", notes = "", response = User.class, tags={ "user", }) @ApiResponses(value = { @ApiResponse(code = 200, message = "successful operation", response = User.class), @ApiResponse(code = 400, message = "Invalid username supplied"), @ApiResponse(code = 404, message = "User not found") }) @RequestMapping(value = "/user/{username}", produces = { "application/xml", "application/json" }, method = RequestMethod.GET) default ResponseEntity
/** * GET /user/login : Logs user into the system * * @param username The user name for login (required) * @param password The password for login in clear text (required) * @return successful operation (status code 200) * or Invalid username/password supplied (status code 400) */ @ApiOperation(value = "Logs user into the system", nickname = "loginUser", notes = "", response = String.class, tags={ "user", }) @ApiResponses(value = { @ApiResponse(code = 200, message = "successful operation", response = String.class), @ApiResponse(code = 400, message = "Invalid username/password supplied") }) @RequestMapping(value = "/user/login", produces = { "application/xml", "application/json" }, method = RequestMethod.GET) default ResponseEntity
/** * GET /user/logout : Logs out current logged in user session * * @return successful operation (status code 200) */ @ApiOperation(value = "Logs out current logged in user session", nickname = "logoutUser", notes = "", tags={ "user", }) @ApiResponses(value = { @ApiResponse(code = 200, message = "successful operation") }) @RequestMapping(value = "/user/logout", method = RequestMethod.GET) default ResponseEntity
/** * PUT /user/{username} : Updated user * This can only be done by the logged in user. * * @param username name that need to be deleted (required) * @param body Updated user object (required) * @return Invalid user supplied (status code 400) * or User not found (status code 404) */ @ApiOperation(value = "Updated user", nickname = "updateUser", notes = "This can only be done by the logged in user.", tags={ "user", }) @ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid user supplied"), @ApiResponse(code = 404, message = "User not found") }) @RequestMapping(value = "/user/{username}", method = RequestMethod.PUT) default ResponseEntity
openapi-generator generate -i ./petstore.yaml -g go-server -o out/server/go-server
main.go
/* * OpenAPI Petstore * * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. * * API version: 1.0.0 * Generated by: OpenAPI Generator (https://openapi-generator.tech) */ package main import ( "log" "net/http" openapi "github.com/GIT_USER_ID/GIT_REPO_ID/go" ) func main() { log.Printf("Server started") PetApiService := openapi.NewPetApiService() PetApiController := openapi.NewPetApiController(PetApiService) StoreApiService := openapi.NewStoreApiService() StoreApiController := openapi.NewStoreApiController(StoreApiService) UserApiService := openapi.NewUserApiService() UserApiController := openapi.NewUserApiController(UserApiService) router := openapi.NewRouter(PetApiController, StoreApiController, UserApiController) log.Fatal(http.ListenAndServe(":8080", router)) }
api.go
/* * OpenAPI Petstore * * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. * * API version: 1.0.0 * Generated by: OpenAPI Generator (https://openapi-generator.tech) */ package openapi import ( "net/http" "os" )
// PetApiRouter defines the required methods for binding the api requests to a responses for the PetApi // The PetApiRouter implementation should parse necessary information from the http request, // pass the data to a PetApiServicer to perform the required actions, then write the service results to the http response. type PetApiRouter interface { AddPet(http.ResponseWriter, *http.Request) DeletePet(http.ResponseWriter, *http.Request) FindPetsByStatus(http.ResponseWriter, *http.Request) FindPetsByTags(http.ResponseWriter, *http.Request) GetPetById(http.ResponseWriter, *http.Request) UpdatePet(http.ResponseWriter, *http.Request) UpdatePetWithForm(http.ResponseWriter, *http.Request) UploadFile(http.ResponseWriter, *http.Request) } // StoreApiRouter defines the required methods for binding the api requests to a responses for the StoreApi // The StoreApiRouter implementation should parse necessary information from the http request, // pass the data to a StoreApiServicer to perform the required actions, then write the service results to the http response. type StoreApiRouter interface { DeleteOrder(http.ResponseWriter, *http.Request) GetInventory(http.ResponseWriter, *http.Request) GetOrderById(http.ResponseWriter, *http.Request) PlaceOrder(http.ResponseWriter, *http.Request) } // UserApiRouter defines the required methods for binding the api requests to a responses for the UserApi // The UserApiRouter implementation should parse necessary information from the http request, // pass the data to a UserApiServicer to perform the required actions, then write the service results to the http response. type UserApiRouter interface { CreateUser(http.ResponseWriter, *http.Request) CreateUsersWithArrayInput(http.ResponseWriter, *http.Request) CreateUsersWithListInput(http.ResponseWriter, *http.Request) DeleteUser(http.ResponseWriter, *http.Request) GetUserByName(http.ResponseWriter, *http.Request) LoginUser(http.ResponseWriter, *http.Request) LogoutUser(http.ResponseWriter, *http.Request) UpdateUser(http.ResponseWriter, *http.Request) }
// PetApiServicer defines the api actions for the PetApi service // This interface intended to stay up to date with the openapi yaml used to generate it, // while the service implementation can ignored with the .openapi-generator-ignore file // and updated with the logic required for the API. type PetApiServicer interface { AddPet(Pet) (interface{}, error) DeletePet(int64, string) (interface{}, error) FindPetsByStatus([]string) (interface{}, error) FindPetsByTags([]string) (interface{}, error) GetPetById(int64) (interface{}, error) UpdatePet(Pet) (interface{}, error) UpdatePetWithForm(int64, string, string) (interface{}, error) UploadFile(int64, string, *os.File) (interface{}, error) }
// StoreApiServicer defines the api actions for the StoreApi service // This interface intended to stay up to date with the openapi yaml used to generate it, // while the service implementation can ignored with the .openapi-generator-ignore file // and updated with the logic required for the API. type StoreApiServicer interface { DeleteOrder(string) (interface{}, error) GetInventory() (interface{}, error) GetOrderById(int64) (interface{}, error) PlaceOrder(Order) (interface{}, error) }
// UserApiServicer defines the api actions for the UserApi service // This interface intended to stay up to date with the openapi yaml used to generate it, // while the service implementation can ignored with the .openapi-generator-ignore file // and updated with the logic required for the API. type UserApiServicer interface { CreateUser(User) (interface{}, error) CreateUsersWithArrayInput([]User) (interface{}, error) CreateUsersWithListInput([]User) (interface{}, error) DeleteUser(string) (interface{}, error) GetUserByName(string) (interface{}, error) LoginUser(string, string) (interface{}, error) LogoutUser() (interface{}, error) UpdateUser(string, User) (interface{}, error) }
1.???????ada
2.???????ada-server
3.???????android
4.???????apache2
5.???????apex
6.???????aspnetcore
7.???????avro-schema
8.???????bash
9.???????c
10.??clojure
11.??cwiki
12.??cpp-qt5-client
13.??cpp-qt5-qhttpengine-server
14.??cpp-pistache-server
15.??cpp-restbed-server
16.??cpp-restsdk
17.??cpp-tizen
18.??csharp
19.??csharp-netcore
20.??csharp-dotnet2
21.??csharp-nancyfx
22.??dart
23.??dart-dio
24.??dart-jaguar
25.??eiffel
26.??elixir
27.??elm
28.??erlang-client
29.??erlang-proper
30.??erlang-server
31.??flash
32.??fsharp-giraffe-server
33.??go
34.??go-experimental
35.??go-server
36.??go-gin-server
37.??graphql-schema
38.??graphql-nodejs-express-server
39.??groovy
40.??kotlin
41.??kotlin-server
42.??kotlin-spring
43.??kotlin-vertx
44.??haskell-http-client
45.??haskell
46.??java
47.??jaxrs-cxf-client
48.??java-inflector
49.??java-msf4j
50.??java-pkmst
51.??java-play-framework
52.??java-undertow-server
53.??java-vertx
54.??java-vertx-web
55.??jaxrs-cxf
56.??jaxrs-cxf-extended
57.??jaxrs-cxf-cdi
58.??jaxrs-jersey
59.??jaxrs-resteasy
60.??jaxrs-resteasy-eap
61.??jaxrs-spec
62.??javascript
63.??javascript-apollo
64.??javascript-flowtyped
65.??javascript-closure-angular
66.??jmeter
67.??k6
68.??lua
69.??mysql-schema
70.??nim
71.??nodejs-server-deprecated
72.??nodejs-express-server
73.??objc
74.??ocaml
75.??openapi
76.??openapi-yaml
77.??perl
78.??php
79.??php-laravel
80.??php-lumen
81.??php-slim-deprecated
82.??php-slim4
83.??php-silex
84.??php-symfony
85.??php-ze-ph
86.??powershell
87.??powershell-experimental
88.??protobuf-schema
89.??python
90.??python-experimental
91.??python-flask
92.??python-aiohttp
93.??python-blueplanet
94.??r
95.??ruby
96.??ruby-on-rails
97.??ruby-sinatra
98.??rust
99.??rust-server
100.???????????scalatra
101.???????????scala-akka
102.???????????scala-finch
103.???????????scala-httpclient-deprecated
104.???????????scala-gatling
105.???????????scala-lagom-server
106.???????????scala-play-server
107.???????????scalaz
108.???????????spring
109.???????????dynamic-html
110.???????????html
111.???????????html2
112.???????????swift2-deprecated
113.???????????swift3-deprecated
114.???????????swift4
115.???????????swift5
116.???????????typescript-angular
117.???????????typescript-angularjs
118.???????????typescript-aurelia
119.???????????typescript-axios
120.???????????typescript-fetch
121.???????????typescript-inversify
122.???????????typescript-jquery
123.???????????typescript-node
124.???????????typescript-redux-query
125.???????????typescript-rxjs
126.???????????asciidoc
127.???????????fsharp-functions
128.???????????markdown
129.???????????scala-sttp
【代碼優先:Swagger UI】
我們來看一下如何從現有的代碼生成API文檔。
安裝程序庫:
API
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。