解决Spring boot中使用Gson,Swagger2 api-docs无法正常显示json问题
2017-03-09 20:07
1271 查看
由于项目中存在自定义类型,而
同时也导致
原因:
解决方法:自定义
接下来注册到
接下来访问
Swagger UI 也正常显示。
问题解决。
Jackson的序列化与反序列化又不太会玩,转而使用
Gson,由于有生成Restful API文档的需求,使用Swagger2,最终
api-docs无法正常显示(使用
Jackson一切正常):
{ "value": "{\"swagger\":\"2.0\",\"info\":{\"description\":\"this is restful api document\",\"version\":\"1.0.0\",\"title\":\"MyApp API文档\",\"contact\":{\"name\":\"xxx\",\"url\":\"http://xxxx.com\",\"email\":\"xxx@xxx.com\"}},\"host\":\"localhost:8080\",\"basePath\":\"/\",\"tags\":[{\"name\":\"login-controller\",\"description\":\"Login Controller\"},{\"name\":\"user-controller\",\"description\":\"User Controller\"}],\"paths\" ......
同时也导致
/swagger-ui.html无法正常显示。
原因:
Gson的序列化问题
解决方法:自定义
Gson序列化
//***注意***:这里的`Json`是`springfox.documentation.spring.web.json.Json`包下的类 class springfoxJsonToGsonAdapter : JsonSerializer<Json> { override fun serialize(json: Json, type: Type, jsc: JsonSerializationContext): JsonElement = JsonParser().parse(json.value()) }
接下来注册到
GsonHttpMessageConverter
//***注意***:`Json`同上,为`springfox.documentation.spring.web.json.Json`包下的类 class IGsonHttpMessageConverter : GsonHttpMessageConverter() { init { //自定义Gson适配器 super.setGson(GsonBuilder() .registerTypeAdapter(Json::class.java, springfoxJsonToGsonAdapter()) .create()) } }
接下来访问
http://localhost:8080/v2/api-docs,得到正常结果
{ "swagger": "2.0", "info": { "description": "this is restful api document", "version": "1.0.0", "title": "MyApp API文档", "contact": { "name": "userName", "url": "http://xxxuri.com", "email": "mail@mailbox.com" } }, "host": "localhost:8080", "basePath": "/", "tags": [{ "name": "login-controller", "description": "Login Controller" }, { "name": "user-controller", "description": "User Controller" }], "paths": { "/login/verify/{userId}": { "get": { "tags": [ "login-controller" ], "summary": "askGroupsAndRoles", "operationId": "askGroupsAndRolesUsingGET", "consumes": [ "application/json" ], "produces": [ "*/*" ], "parameters": [{ "name": "userId", "in": "path", "description": "userId", "required": true, "type": "string" }], "responses": { "200": { "description": "OK" }, "401": { "description": "Unauthorized" }, "403": { "description": "Forbidden" }, "404": { "description": "Not Found" } } } } }
Swagger UI 也正常显示。
问题解决。
相关文章推荐
- 在使用Gson的toJson()方法转为json字符串的时候无法显示中文和符号的问题(已解决)
- freetype使用日记 -- 解决宋体,楷体中文在某些字号下无法正常显示的问题
- 解决Spring boot使用Quartz时Job中无法注入Bean 空指针异常问题
- freetype使用日记 -- 解决宋体,楷体中文在某些字号下无法正常显示的问题
- 解决Springboot使用FastJson 返回中文乱码的问题
- Spring boot集成Swagger2,解决页面不显示的问题
- 关于使用Volley下载的json字符串无法显示中文的问题(已解决)
- IntelliJ IDEA中Spring Boot项目使用spring-boot-devtools无法实现热部署/热更新的问题解决
- 自行解决12306页面显示异常的问题(长城宽带下WWW。12306无法正常使用)
- spring boot新手教程之使用FastJson解析JSON数据以及解决返回中文乱码问题
- 多种方式解决spring boot swagger ui使用 nginx 部署后无法使用问题
- [Android]浮层视频效果,在另外一个Window使用SurfaceView无法正常显示的问题排查与解决
- python2.7:dic2json 解决中文无法正常显示问题
- Android Service中使用Toast无法正常显示问题的解决方法
- Springboot使用FastJson后,接口返回中文乱码的问题解决。
- 解决Jekyll代码块无法正常显示Liquid代码问题
- 【问题解决】windows程序使用LaodImage加载图标无法显示在程序窗口标题栏
- VS调试问题,程序无法正常启动 (使用openFlight_API中遇到的问题)
- Spring Boot下无法加载主类 org.apache.maven.wrapper.MavenWrapperMain问题解决
- Ubuntu14.04使用VNC无法显示图形界面问题的解决