关于springboot返回类型设置ContentType属性

最近做富文本编辑器ueditor,遇到一个问题加载JS文件报错,Refused to execute script from because its MIME type (text/plain) is not executable, and strict MIME type checking is enabled...

这个意思是说,动态加载js文件时返回Content-Type为text/plain,与文件类型.js不匹配,禁止加载。

解决办法:

                在类上加注解@RestController,在方法上加注解@ResponseBody,这个是必要条件,但想要设置ContentType要在@RequestMapping注解上加一个属性produces=“application/json,charset=utf-8”,加上这个属性就完美解决了。

对于这个属性的解释百度了一下,它的作用是指定返回值类型,还可以设置返回值的编码,与之对应还有一个属性,是consumes,但它仅处理request Content-Type为“application/json”类型的请求。

关于springboot返回类型设置ContentType属性