Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass
作者:互联网
之前遇到jdk版本升级,新版本的JDK对一些反射相关的功能进行了限制从而引发的问题,我自己遇到的是在上传excel文件失败,开始以为是文件上传方式的问题,检测无误后还是没解决。
1.下面是controller
@PostMapping(value = "/importExcel", consumes = "multipart/*", headers = "content-type=multipart/form-data") @ResponseBody public void importExcel(@RequestPart(value = "file") MultipartFile file) throws Exception {
consumes = "multipart/*" 加不加这个也没啥用。
@RequestParam或者@RequestPart都可以
2.网络请求-标头
请求 URL: http://localhost:8080/api/merchantscarrier/importExcel 请求方法: POST 状态代码: 415 远程地址: [::1]:8080 引用站点策略: strict-origin-when-cross-origin Accept: multipart/form-data, multipart/* Access-Control-Allow-Credentials: true Access-Control-Allow-Origin: http://localhost:8000 Cache-Control: no-cache, no-store, max-age=0, must-revalidate Connection: keep-alive Content-Encoding: gzip Content-Type: application/json Date: Fri, 17 Jun 2022 09:06:21 GMT Expires: 0 Keep-Alive: timeout=60 Pragma: no-cache Transfer-Encoding: chunked Vary: origin,access-control-request-method,access-control-request-headers,accept-encoding X-Content-Type-Options: nosniff X-Frame-Options: DENY X-XSS-Protection: 1; mode=block Accept: application/json Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9 Authorization: Bearer eyJhbGciOiJIUzIXXX Connection: keep-alive Content-Length: 2 Content-Type: application/json;charset=UTF-8 Host: localhost:8080 Origin: http://localhost:8000 Referer: http://localhost:8000/ sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="102", "Microsoft Edge";v="102" sec-ch-ua-mobile: ?0 sec-ch-ua-platform: "Windows" Sec-Fetch-Dest: empty Sec-Fetch-Mode: cors Sec-Fetch-Site: same-site User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.124 Safari/537.36 Edg/102.0.1245.41
3.响应的错误信息
2022-06-17 17:05:44 WARN o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported]
从网页测试根本行不通,我用apipostman测试,文件可以过来但是报新的错误
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @6a400542
4.解决
这是由于 JDK 8 中有关反射相关的功能自从 JDK 9 开始就已经被限制了,为了兼容原先的版本,需要在运行项目时添加 --add-opens java.base/java.lang=ALL-UNNAMED
选项来开启这种默认不被允许的行为。
--add-opens java.base/java.lang=ALL-UNNAMED
除了添加选项启动选项之外,由于原本的项目是在 JDK 8 的环境中运行的,此时需要将运行的 JDK 转变为 JDK 11,具体做法(仅针对 Maven 项目):将父 pom.xml 文件中的 <java.version> 标签中的 java 版本修改为需要运行的版本,具体如下所示:
<properties> <java.version>17</java.version> </properties>
标签:lang,java,JDK,make,Content,multipart,localhost 来源: https://www.cnblogs.com/sx-jd/p/16386778.html