SpringBoot 读取远程 js 文件 返回给前端使用,但是 Content-Type 变成 application/json
时间: 2024-02-17 14:00:53 浏览: 28
您好!如果在 SpringBoot 读取远程 js 文件后返回给前端使用时,Content-Type 变成了 application/json,这可能是因为 SpringBoot 默认使用了 Jackson 库将返回值序列化成 JSON 格式。
要解决这个问题,可以尝试以下两种方法:
1. 在 Controller 中设置返回值类型为 String,手动设置 Content-Type 的值为 text/javascript。例如:
```java
@GetMapping("/getRemoteJs")
public ResponseEntity<String> getRemoteJs() throws Exception {
String remoteJs = // 读取远程 js 文件的内容
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.valueOf("text/javascript"));
return new ResponseEntity<>(remoteJs, headers, HttpStatus.OK);
}
```
在上述代码中,将返回值类型设置为 String,并手动设置 Content-Type 的值为 text/javascript。
2. 在配置文件中关闭 Jackson 库的自动序列化功能。例如:
```properties
spring.jackson.enabled=false
```
在上述配置中,将 spring.jackson.enabled 配置项的值设置为 false,可以关闭 Jackson 库的自动序列化功能,从而避免返回值被序列化成 JSON 格式。
希望能帮到您!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)