MockHttpServletRequest中的奇怪字符Swagger2 DeferredResultÂresponse

时间:2016-05-10 21:29:51

标签: spring-mvc character-encoding swagger-2.0 spring-mvc-test

运行验证测试并将这些讨厌的字符嵌入Swagger 2.0验证输出中 {"$ref":"#/definitions/DeferredResult«ResponseEntity«SummCollection»»"

控制器方法返回:DeferredResult<ResponseEntity<SummCollection>>

我刚刚注意到我在写这个问题:实际:“&lt;”疯狂:“”“

MockHttpServletRequest中的奇怪字符Swagger2 DeferredResultÂ响应

这似乎是“iso-8859-1”与“utf-8”之间的问题吧? IDK。

问题是什么导致SwaggerTest validateImplementationAgainstDesignSpec读取文件.yaml json就好了,但是MockMvc执行GET响应是包含类名和分隔符之间的这些“”字符。

CODE CALL:

MvcResult mvcResult = this.mockMvc.perform(get("/v2/api-docs?group=full-api") // 2 .accept(MediaType.APPLICATION_JSON) .characterEncoding("application/json;charset=ISO-8859-1")) .andDo(MockMvcResultHandlers.print()) .andExpect(status().isOk()) .andReturn();

我调试了代码,行.characterEncoding("application/json;charset=ISO-8859-1"))设置为“UTF-8”,其他一切都在极端(包括“windows-1252”)。这发生在Linux CI服务器上,因此它不是卷绕器。

我已经读过这是一个StringHttpMessageConverter问题/症状,但无法解决使用MockMVC调用Swagger2的问题。

输出:
MockHttpServletResponse: Status = 200 Error message = null Headers = {Content-Type=[application/json]} Content type = application/json Body = {"swagger":"2.0","info":{"description":"Some Description ","version":"1.0","title":"Sum-Server","license":{}},"host":"localhost","basePath":"/","tags":[{"name":"summ-service","description":"Summary Service"}],"paths":{"/api/v1/summ/{id}":{"get":{"tags":["device-summary-service"],"summary":"getDeviceSummaries","operationId":"getDeviceSummariesUsingGET","consumes":["application/json"],"produces":["application/json"],"parameters":[{"name":"id","in":"path","description":"id","required":true,"type":"string"},{"name":"days","in":"query","description":"days","required":false,"type":"string"},{"name":"start","in":"query","description":"start","required":false,"type":"string"},{"name":"end","in":"query","description":"end","required":false,"type":"string"}],"responses":{"200":{"description":"OK","schema":{"$ref":"#/definitions/DeferredResult«ResponseEntity«SummCollection»»"}}}}}},"definitions":{"SummCollection":{"type":"object","properties":{"SummList":{"type":"array","items":{"$ref":"#/definitions/SummDateRange"}},"endDate":{"type":"string"},"prioritizedReportFamily":{"type":"integer","format":"int32"},"startDate":{"type":"string"}}},"DeferredResult«ResponseEntity«SummCollection»»":{"type":"object","properties":{"result":{"type":"object"},"setOrExpired":{"type":"boolean"}}}}}

1 个答案:

答案 0 :(得分:1)

每当我提出一个问题,浪费时间,对自己说,不要做一个堆叠溢出,你几乎拥有它。然后在绝望中,最后写下这个问题......

35分钟后,找到答案......也很简单。 {哎哟} 看这里: http://docs.spring.io/spring/docs/current/javadoc-api/index.html?constant-values.html

APPLICATION_JSON_UTF8_VALUE "application/json;charset=UTF-8" APPLICATION_JSON_VALUE "application/json"

修复是:

.accept(MediaType.APPLICATION_JSON_UTF8_VALUE))

MediaType !哈!希望这有助于某人。 :)