• Json反序列化遇到的问题


    //这是我的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啊

     

  • 相关阅读:
    Element库的Vue版本ElementUI的本地引入方法
    在Win7操作系统上安装VS2017报错:安装程序清单签名验证失败
    [转]五大主流浏览器及四大内核
    [转]idea2021.1破解版 附安装教程免激活码
    [转]Node.js安装详细步骤教程(Windows版)
    [转]Windows系统下彻底删除Windows.old 文件夹的方法
    Springboot+Vue进行Web开发时特别需要注意的小事项
    基带信号与频带信号,基带传输与频带传输各是什么?两者有什么区别?
    springboot的Web项目编译运行时提示错误:Field userService in com.cetc.UserManger.controller.UserController required a bean of type 'com.cetc.UserManger.service.UserService' that could not be found.
    创建springboot项目时出现Selected Java version 11 is not supported by SDK (maximum 8)
  • 原文地址:https://www.cnblogs.com/joker1937/p/10418514.html
Copyright © 2020-2023  润新知