【发布时间】:2017-11-26 10:35:38
【问题描述】:
我实现了一个 REST API,如下所示。浏览器 - 作为客户端 - 无法执行我从提到的 REST 给出的 javascript 并打印错误,例如 Refused to execute script from 'http://localhost:8083/vendor.js' because its MIME type ('') is not executable, and strict MIME type checking is enabled。这意味着当我为响应设置内容类型时,没有为响应设置 MIME 类型,但它被应用了。
@RequestMapping(value = "/{resoucePath:.+}", method = RequestMethod.GET)
public void getResouce(@PathVariable String resoucePath, HttpServletResponse response) {
String myPath = ...; // I set resource path here
File resouce = new File(myPath);
try {
InputStream is = new FileInputStream(resouce);
IOUtils.copy(is, response.getOutputStream());
response.setContentType(mimeTypesMap.getContentType(resouce));
response.flushBuffer();
} catch (IOException ex) {
// log error
throw new RuntimeException("IOError writing file to output stream", ex);
}
}
我的错误是什么?为什么没有在响应中设置 content-type?
【问题讨论】:
-
@harshavmb 接受了对上述问题的回复,建议使用为 RequestMapping 生成属性,但我的 mime 类型是动态的。我的情况可以通过这个答案来解决:stackoverflow.com/a/4471956/5538979。我的代码类似于这个答案,但它不起作用!
标签: javascript java spring rest