1

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

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

我只是在写问题时注意到:实际:“<”疯狂:“«”

MockHttpServletRequest Swagger2 DeferredResult  响应中的奇怪字符

这似乎是“iso-8859-1”与“utf-8”之间的问题,对吧?身份证。

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

代码调用:

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"}}}}}

4

1 回答 1

3

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

35 分钟后,找到答案.... 也很简单。{ouch} 见这里: 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))

媒体类型!哈!希望这可以帮助某人。:)

于 2016-05-10T22:17:55.373 回答