【Azure API 管理】通过Java APIM SDK创建一个新的API,如何为Reqeust的Representation设置一个内容示例(Sample)? - LuBu0505/My-Code GitHub Wiki

问题描述

在使用API Management服务时,以Echo API(默认创建)举例,它会在Request的body部分默认设置一个SAMPLE指,这样在测试接口时候,就会有默认的Body内容,我们只需要根据JSON值就可以了。测试非常便捷。 image.png

那么,如果需要通过Java APIM的SDK来创建一个新接口,并且为它设置SAMPLE值,如何来实现呢?

问题解答

查看源代码文件,可以使用 withExamples 方法来实现 public RepresentationContract withExamples(Map<String,ParameterExampleContract> examples)

源文件路径https://docs.microsoft.com/en-us/java/api/com.azure.resourcemanager.apimanagement.models.representationcontract.withexamples?view=azure-java-preview image.png

示例代码包含三部分

**第一部分:**以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 类的定义截图 image.png

**附录二:**创建成功后的截图 image.png

参考资料

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 管理】

标签: APIMAPIM JAVA Create APIMap<string, parameterexamplecontract="" style="margin: 0px; padding: 0px;"> map</string,>RepresentationContract withExamples

⚠️ **GitHub.com Fallback** ⚠️