問題描述
在使用API Management服務時,以Echo API(默認創建)舉例,它會在Request的body部分默認設置一個SAMPLE指,這樣在測試接口時候,就會有默認的Body內容,我們只需要根據JSON值就可以了。測試非常便捷。
那么,如果需要通過Java APIM的SDK來創建一個新接口,并且為它設置SAMPLE值,如何來實現呢?
問題解答
查看源代碼文件,可以使用 withExamples 方法來實現 public RepresentationContract withExamples(Map<String,ParameterExampleContract> examples)
示例代碼包含三部分
第一部分:以Vehicle為原型,生成ParameterExampleContract對象
Vehicle vehicle = new Vehicle().withVehicleType("vehicleType").withSpeedUnit("KM").withMaxSpeed(125).withAvgSpeed(90);
ParameterExampleContract parameterExample = new ParameterExampleContract().withValue(vehicle);
第二部分:把第一部分生成的對象,加入到Map對象
Map<String, ParameterExampleContract> map = new HashMap<String, ParameterExampleContract>();
map.put("default", parameterExample);
注意:一定要設置Map的Key,第一個參數一定要為 default。
第三部分:在APIM的manager創建函數中使用map對象
manager
.apiOperations()
.define("newoperations")
.withExistingApi("rg1", "apimService2", "echo-api")
.withDisplayName("test sample")
.withMethod("POST")
.withUrlTemplate("/user1")
.withTemplateParameters(Arrays.asList())
.withDescription("This can only be done by the logged in user.")
.withRequest( new RequestContract()
.withDescription("Created user object")
.withQueryParameters(Arrays.asList())
.withHeaders(Arrays.asList())
.withRepresentations(
Arrays
.asList( new RepresentationContract()
.withContentType("application/json")
.withExamples(map)
.withTypeName("User"))))
.withResponses(
Arrays.asList( new ResponseContract()
.withStatusCode(200)
.withDescription("successful operation")
.withRepresentations(
Arrays.asList( new RepresentationContract()
.withContentType("application/xml"), new RepresentationContract()
.withContentType("application/json")))
.withHeaders(Arrays.asList())))
.create();
附錄一:第一部分中Vehicle 類的定義截圖
附錄二:創建成功后的截圖
參考資料
Azure Resource Manager ApiManagement client library for Java: https://docs.microsoft.com/en-us/java/api/overview/azure/resourcemanager-apimanagement-readme?view=azure-java-preview
RepresentationContract.withExamples(Map<String,ParameterExampleContract> examples) Method : https://docs.microsoft.com/en-us/java/api/com.azure.resourcemanager.apimanagement.models.representationcontract.withexamples?view=azure-java-preview
當在復雜的環境中面臨問題,格物之道需:濁而靜之徐清,安以動之徐生。 云中,恰是如此!
分類: 【Azure API 管理】
標簽: APIM, APIM JAVA Create API, Map<string, parameterexamplecontract="" style="margin: 0px; padding: 0px;"> map</string,>, RepresentationContract withExamples