1. 程式人生 > >springboot介面返回資料型別解析問題

springboot介面返回資料型別解析問題

問題:今天在使用postman除錯springboot專案的介面的時候一直報錯提示:

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

但是我在controller裡已經寫了@ResponseBody標籤,controller裡使用的是@Controller標籤

結果在使用postman測試的時候一直是{ status:500; msg:"could not find acceptable representation"};

在網上看了很多種方法有的是在pom.xml裡新增其他json包的依賴等等。。。無果,,,,

解決:

最終success解決了;

在全域性攔截器裡繼承WebMvcConfigurerAdapter然後重寫configureContentNegotiation方法,在方法裡重寫下:

super.configureContentNegotiation(configurer);
configurer.defaultContentType(MediaType.APPLICATION_JSON_UTF8);
configurer.favorPathExtension(false);

這裡千萬注意一定要先呼叫super父類的方法,然後再寫預設的返回資料格式(今天就是因為在這裡順序後呼叫父類的方法,導致一直是失敗的)!!!

同時你也可以在啟動類裡繼承WebMvcConfigurerAdapter去重寫;

問題解決,希望這次踩過的坑可以幫助大家!