实现 ResponseBodyAdvice
拦截了 接口 的返回数据,对返回数据进行封装,这样可以保持一个统一的模板格式给前端,易于前端处理,同时,简化后端每个接口都需要手动构建返回格式的维护等

ResponseTemplate

Hello World

此时请求 hello接口,会报ResponseTemplate不能转为string的异常
解决

重写WebMvcConfigurerAdapter,覆盖了原有的HttpMessageConverters,此处采用:com.alibaba.fastjson
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
新问题
解决了转换异常,又遇到中文乱码
再解决
配置文件添加
spring:
http:
encoding:
charset: UTF-8
force: true
enabled: true
messages:
encoding: UTF-8
banner:
charset: UTF-8
效果

网友评论