0

運行驗證測試,並得到嵌入揚鞭2.0驗證輸出這些討厭的人物 {"$ref":"#/definitions/DeferredResult«ResponseEntity«SummCollection»»"性格怪異響應

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

我只注意到我在寫問題:實際: 「<」 瘋狂:「一«」

奇怪的字符MockHttpServletRequest Swagger2 DeferredResult響應

這似乎是「iso-8859-1」和「utf-8」之間的問題吧? IDK。

問題是什麼導致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」,一切都在極度其他(包括 「窗口1252」)。這發生在Linux CI服務器上,所以它不是一個winders thang。

我讀過關於這是一個StringHttpMessageConverter問題/症狀,但無濟於事如何解決使用MockMVC來調用Swagger2。

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

回答

0

我每次做的問題,浪費時間,對自己說,不要做一個堆棧溢出,你幾乎有它。然後在絕望中,最後寫下這個問題...

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!哈!希望這有助於某人。 :)