//这是我的swagger01.json 下面的代码针对这个json进行反序列化
{
"swagger": "2.0",
"info": {
"description": "这是一个swagger2小型demo",
"version": "0.0.1",
"title": "springboot结合swagger2构建Restful API",
"termsOfService": "www.baidu.com",
"contact": { "name": "xujiayi" }
},
"host": "localhost:8080",
"basePath": "/",
"tags": [
{
"name": "user-controller",
"description": "User Controller"
}
],
"paths": {
"/users": {
"get": {
"tags": [ "user-controller" ],
"summary": "获取用户列表",
"operationId": "getListUsingGET",
"consumes": [ "application/json" ],
"produces": [ "*/*" ],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": { "$ref": "#/definitions/User" }
}
},
"401": { "description": "Unauthorized" },
"403": { "description": "Forbidden" },
"404": { "description": "Not Found" }
}
},
"post": {
"tags": [ "user-controller" ],
"summary": "创建用户",
"description": "根据user对象创建用户",
"operationId": "postUserUsingPOST",
"consumes": [ "application/json" ],
"produces": [ "*/*" ],
"parameters": [
{
"in": "body",
"name": "user",
"description": "用户信息实体类",
"required": true,
"schema": { "$ref": "#/definitions/User" }
}
],
"responses": {
"200": {
"description": "OK",
"schema": { "type": "string" }
},
"201": { "description": "Created" },
"401": { "description": "Unauthorized" },
"403": { "description": "Forbidden" },
"404": { "description": "Not Found" }
}
}
},
"/users/{id}": {
"get": {
"tags": [ "user-controller" ],
"summary": "获取用户信息",
"description": "根据url的id来获取用户信息",
"operationId": "getUserByIdUsingGET",
"consumes": [ "application/json" ],
"produces": [ "*/*" ],
"parameters": [
{
"name": "id",
"in": "path",
"description": "用户id",
"required": true,
"type": "integer",
"format": "int64"
}
],
"responses": {
"200": {
"description": "OK",
"schema": { "$ref": "#/definitions/User" }
},
"401": { "description": "Unauthorized" },
"403": { "description": "Forbidden" },
"404": { "description": "Not Found" }
}
},
"put": {
"tags": [ "user-controller" ],
"summary": "更新用户信息",
"description": "根据url的id来指定对象,并且根据传过来的user进行用户基本信息更新",
"operationId": "putUserUsingPUT",
"consumes": [ "application/json" ],
"produces": [ "*/*" ],
"parameters": [
{
"name": "id",
"in": "path",
"description": "用户id",
"required": true,
"type": "integer",
"format": "int64"
},
{
"in": "body",
"name": "user",
"description": "用户信息实体类user",
"required": true,
"schema": { "$ref": "#/definitions/User" }
}
],
"responses": {
"200": {
"description": "OK",
"schema": { "type": "string" }
},
"201": { "description": "Created" },
"401": { "description": "Unauthorized" },
"403": { "description": "Forbidden" },
"404": { "description": "Not Found" }
}
},
"delete": {
"tags": [ "user-controller" ],
"summary": "删除用户",
"description": "根据url的id来指定对象,进行用户信息删除",
"operationId": "delUserUsingDELETE",
"consumes": [ "application/json" ],
"produces": [ "*/*" ],
"parameters": [
{
"name": "id",
"in": "path",
"description": "用户id",
"required": true,
"type": "integer",
"format": "int64"
}
],
"responses": {
"200": {
"description": "OK",
"schema": { "type": "string" }
},
"401": { "description": "Unauthorized" },
"204": { "description": "No Content" },
"403": { "description": "Forbidden" }
}
}
}
},
"definitions": {
"User": {
"type": "object",
"properties": {
"age": {
"type": "integer",
"format": "int32"
},
"id": {
"type": "integer",
"format": "int64"
},
"name": { "type": "string" }
}
}
}
}
反序列化代码:
1 @org.junit.Test 2 public void readSwaggerJson() { 3 ObjectMapper objectMapper = new ObjectMapper(); 4 File json = new File("C:\Users\45316\Desktop\swagger01.json"); 5 try { 6 Map<String, Map<String, Map<String, Map<String, List<Map<String, Map<String, String>>>>>>> map = objectMapper.readValue(json, Map.class); 7 System.out.println(map.get("definitions")); 8 System.out.println(map.get("paths").get("/users").get("post").get("responses").getClass()); 9 } catch (JsonParseException e) { 10 e.printStackTrace(); 11 } catch (JsonMappingException e) { 12 e.printStackTrace(); 13 } catch (IOException e) { 14 e.printStackTrace(); 15 } 16 }
想问下大家,map.get("paths").get("/users").get("post").get("responses")这里都能返回对象,一加上getClass()就报类型转换异常,我知道get("responses")返回的不是List类型,但我就单纯的想得到对象所对应的类型为什么不可以?我并没有把LinkedHashMap强转为List啊