java 生成客户端代码_swagger-codegen生成java客户端代码
前后端分離的時候,需要建立契約,Swagger可達到該目的(略)。
建立Rest接口后,通過swagger-codegen項目可以自動生成對應的客戶端代碼(c++、php、java、js、node等等),
關于swagger-codegen項目的使用,發現中文文檔較少,追根述源后,發現其中相當一部分同出一轍。
親自摸索使用后,記錄一下步驟,利己利人。
swagger-codegen項目地址: https://github.com/swagger-api/swagger-codegen
項目文檔(README.md)說的比較詳細,但是重復的地方太多,容易讓新手搞暈。
其實,文檔內容可以簡化為如下步驟(凡‘略’,皆不予理睬):
【注意】以下步驟,均在Windows系統中操作,生成java客戶端代碼,其他語言類似;英文不好,有些不會翻譯,故保留英文原文;
1、Table of contents(略)
2、Compatibility(略)
3、Prerequisites
3.1、從maven倉庫下載swagger-codegen的jar文件,如下
io.swagger
swagger-codegen
2.2.1
放到本地目錄(比如D:\env\swagger-codegen-cli-2.2.1.jar);
3.2、本機安裝JDK7+,并設置$PATH(比如:D:\Java\jdk1.8.0_60\bin;);
3.3、本機安裝Maven3.3.3+,并設置$PATH(比如:D:\apache-maven-3.3.3\bin;);
4、Building(略)
5、Docker(略)
6、Homebrew(略)
7、Getting Started
dos窗口中,執行如下命令(Windows系統下,換行符無效,必須寫在同一行):
java -jar D:\env\swagger-codegen-cli-2.2.1.jar generate
-i http://petstore.swagger.io/v2/swagger.json
-l java
-o D:\swagger-client
--model-package com.xxx.client.model
--api-package com.xxx.client.api
【參數說明】
-jar 指定 swagger-codegen-cli-2.2.1.jar 的位置,絕對路徑、相對路徑均可;
-i 指定 swagger.json 的位置,本地路徑、網絡路徑均可;
-l 指定客戶端代碼的語言;
-o 指定代碼生成的位置;
--model-package 指定model代碼的包名;
--api-package 指定api代碼的包名;
8、Workflow Integration(略)
9、Github Integration(略)
【補充后續操作步驟】
10、將生成的代碼拷貝到項目中(api、model、test)
11、pom.xml添加依賴(僅操作一次)
io.swagger
swagger-jaxrs
1.5.9
io.swagger
swagger-annotations
1.5.9
com.squareup.okhttp
okhttp
2.7.5
com.squareup.okhttp
logging-interceptor
2.7.5
com.google.code.gson
gson
2.6.2
joda-time
joda-time
2.9.3
12、修改ApiClient.java(一般來說,該文件無須覆蓋,僅操作一次)
12.1、修改屬性 basePath的值,自己的api根路徑;
12.2、如果需要ssl,或者url追加token,需要修改 buildUrl()方法;
13、執行測試代碼即可(生成的測試代碼,方法體是空的,需要自己補充代碼);
【補充】
A、下載源代碼:
下載最新代碼
> git clone https://github.com/swagger-api/swagger-codegen.git
下載指定版本
> git clone --branch v2.2.1 https://github.com/swagger-api/swagger-codegen.git
B、查看swagger-codegen支持的命令
> java -jar swagger-codegen-cli-2.2.1.jar help
用法: swagger-codegen-cli []
常用命令如下:
config-help ? 指定客戶端語言的可選項說明
generate ? ? ?生成客戶端代碼命令
help ? ? ? ? ?幫助
langs ? ? ? ? 顯式所有支持的語言
meta ? ? ? ? ?MetaGenerator.?Generator for creating a new template set and configuration for Codegen. ?The output will be based on the language you specify, and includes default templates to include.
See 'swagger-codegen-cli help ' for more information on a specific command.
C、查看swagger-codegen的generate命令的可選項
> java -jar swagger-codegen-cli-2.2.1.jar help generate
NAME
swagger-codegen-cli generate - Generate code with chosen lang
SYNOPSIS
swagger-codegen-cli generate [options]
OPTIONS
-a
當獲取遠程swagger定義時,添加授權頭信息;URL-encoded格式化的name,逗號隔開的多個值;
--auth
同 -a;
--additional-properties
設置額外的屬性供模版使用;格式為name=value,name=value;
--api-package
指定生成的api類的包名;
--artifact-id
指定pom.xml的artifactId的值;
--artifact-version
指定pom.xml的artifact的版本;
-c
json格式的配置文件的路徑;文件為json格式,支持的配置項因語言的不同而不同;
執行 config-help -l {lang} 命令查看詳細的配置項;
--config
同 -c;
-D
指定系統屬性,格式為name=value,name=value;
--group-id
指定pom.xml的groupId的值;
-i
指定swagger描述文件的路徑,url地址或路徑文件;該參數為必須;
--input-spec
同 -i;
--import-mappings
specifies mappings between a given class and the import that should be used for that class,
格式type=import,type=import;
--instantiation-types
設置實例類型映射,格式type=instantiatedType,type=instantiatedType.
例如(in Java):array=ArrayList,map=HashMap.換句話說,生成代碼時,array類型會實例化為ArrayList;
--invoker-package
指定根包名稱;
-l
指定生成客戶端代碼的語言,該參數為必須;
--lang
同 -l;
--language-specific-primitives
指定附加語言特定的原始類型,格式type1,type2,type3,type3.例如:String,boolean,Boolean,Double;
--library
library template (sub-template);
--model-package
指定生成的model類的包名;
-o
指定生成文件的位置(默認當前目錄);
--output
同 -o;
-s
指定該參數表示不覆蓋已經存在的文件;
--skip-overwrite
同 -s;
-t
指定模版文件所在目錄;
--template-dir
同 -t;
--type-mappings
設置swagger類型與生成的代碼類型之間的映射,格式swaggerType=generatedType,swaggerType=generatedType.
例如: array=List,map=Map,string=String;
-v
指定verbose mode;
--verbose
同 -v;
D、查看java客戶端的可配置項
> java -jar swagger-codegen-cli-2.2.1.jar config-help -l java
CONFIG OPTIONS
sortParamsByRequiredFlag
Sort method arguments to place required parameters before optional parameters. (Default: true)
ensureUniqueParams
Whether to ensure parameter names are unique in an operation (rename parameters that are not). (Default: true)
modelPackage
package for generated models
apiPackage
package for generated api classes
invokerPackage
root package for generated code
groupId
groupId in generated pom.xml
artifactId
artifactId in generated pom.xml
artifactVersion
artifact version in generated pom.xml
sourceFolder
source folder for generated code
localVariablePrefix
prefix for generated code members and local variables
serializableModel
boolean - toggle "implements Serializable" for generated models (Default: false)
bigDecimalAsString
Treat BigDecimal values as Strings to avoid precision loss. (Default: false)
fullJavaUtil
whether to use fully qualified name for classes under java.util.
This option only works for Java API client (Default: false)
hideGenerationTimestamp
hides the timestamp when files were generated
dateLibrary
Option. Date library to use
joda - Joda
legacy - Legacy java.util.Date
java8-localdatetime - Java 8 using LocalDateTime (for legacy app only)
java8 - Java 8 native
useRxJava
Whether to use the RxJava adapter with the retrofit2 library. (Default: false)
library
library template (sub-template) to use (Default: okhttp-gson)
jersey1 - HTTP client: Jersey client 1.19.1. JSON processing: Jackson 2.7.0
feign - HTTP client: Netflix Feign 8.16.0. JSON processing: Jackson 2.7.0
jersey2 - HTTP client: Jersey client 2.22.2. JSON processing: Jackson 2.7.0
okhttp-gson - HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.6.2
retrofit - HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.3.1 (Retrofit 1.9.0).
IMPORTANT NOTE: retrofit1.x is no longer actively maintained so please upgrade to 'retrofit2' instead.
retrofit2 - HTTP client: OkHttp 3.2.0. JSON processing: Gson 2.6.1 (Retrofit 2.0.2).
Enable the RxJava adapter using '-DuseRxJava=true'. (RxJava 1.1.3)
E、查看swagger-codegen支持的客戶端語言
> java -jar swagger-codegen-cli-2.2.1.jar langs
(結果略)
或參考 https://github.com/swagger-api/swagger-codegen/wiki/API-client-generator-HOWTO
F、服務器端代碼生成,參考https://github.com/swagger-api/swagger-codegen/wiki/Server-stub-generator-HOWTO
G、swagger支持的數據類型映射
http://swagger.io/specification/
參考代碼:DefaultCodegen.java
this.typeMapping = new HashMap();
this.typeMapping.put("array", "List");
this.typeMapping.put("map", "Map");
this.typeMapping.put("List", "List");
this.typeMapping.put("boolean", "Boolean");
this.typeMapping.put("string", "String");
this.typeMapping.put("int", "Integer");
this.typeMapping.put("float", "Float");
this.typeMapping.put("number", "BigDecimal");
this.typeMapping.put("DateTime", "Date");
this.typeMapping.put("long", "Long");
this.typeMapping.put("short", "Short");
this.typeMapping.put("char", "String");
this.typeMapping.put("double", "Double");
this.typeMapping.put("object", "Object");
this.typeMapping.put("integer", "Integer");
this.typeMapping.put("ByteArray", "byte[]");
this.typeMapping.put("binary", "byte[]");
遺留問題:
1、還不清楚如何將api數據類型和java的數據類型進行映射;
--------------------- 本文來自 wangjunjun2008 的CSDN 博客 ,全文地址請點擊:https://blog.csdn.net/wangjunjun2008/article/details/53200437?utm_source=copy
總結
以上是生活随笔為你收集整理的java 生成客户端代码_swagger-codegen生成java客户端代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: e x泰勒 java_maven pro
- 下一篇: 如何用计算机装手机系统,如何用手机usb