開發踩坑紀實
- 1 npm安裝
- 1.1 查看當前的npm鏡像設置
- 1.2 清空緩存
- 1.3 修改鏡像
- 1.4 查看修改結果
- 1.5 重新安裝vue
- 2 VScode——NPM腳本窗口找不到
- 3 springboot項目中updateById()失效
- 4 前端跨域
- 4.1 后端加個配置類
- 4.2 @CrossOrigin注解
- 5 路由出口
- 6 springdoc openapi3 swagger3文件上傳
- 7 連接mysql時出現[HY000][1130] null, message from server: “Host ‘‘ is not allowed報錯
- 8 Nacos2.2.3 啟動成功但訪問空白
- 9 ERROR StatusLogger Log4j2 could not find a logging imple mentation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
- 10 mapper.xml在pom文件中打包后找不到數據源配置了
- 11 Refused to execute script from because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled
- 12 Nacos Whitelabel Error Page 503
- 13 Mybatis-Plus代碼生成器
- 14 @RequestBody
1 npm安裝
npm報錯:request to https://registry.npm.taobao.org failed, reason certificate has expired
其實,早在2021年,淘寶就發文稱,npm淘寶鏡像已經從registry.npm.taobao.org切換到了registry.npmmirror.com。舊域名也將于2022年5月31日停止服務(不過,直到今天HTTPS證書到期才真正不能用了)
解決方案
1.1 查看當前的npm鏡像設置
npm config list
1.2 清空緩存
npm cache clean --force
1.3 修改鏡像
npm config set registry https://registry.npmmirror.com
1.4 查看修改結果
1.5 重新安裝vue
npm install -g @vue/cli #需要管理權限
vue3官網推薦在項目目錄下執行:
npm create vue@latest
2 VScode——NPM腳本窗口找不到
設置→User→Extensions→Npm→勾選Enable Run From Folder
點擊項目右上角的...
,勾選NPM Scripts
3 springboot項目中updateById()失效
實體類繼承自公共實體類BaseEntity,并將主鍵id放進去了,service方法調用getById正常,但是調用updateById時,傳入一個json實體卻獲取不到id,這時,在id上添加注解:@JsonProperty("id")
@JsonProperty(“id”) //入參轉換,為解決前端傳入的json包無法接收
@JsonFiels(name = “id”) //出參轉換
4 前端跨域
http://127.0.0.1:8001/admin/edu/teacher/list/1/5’ from origin ‘http://localhost:9528’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
4.1 后端加個配置類
@Configuration
public class CorsConfig implements WebMvcConfigurer {@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**").allowedOriginPatterns("*") // 允許跨域的域名,*代表任何域名.allowedMethods("GET", "POST") // 允許的方法.allowedHeaders("*") // 允許任何請求頭.allowCredentials(true) // 允許cookies/*.exposedHeaders(HttpHeaders.of("SET_COOKIE",fie))*/.maxAge(3600L);}
}
4.2 @CrossOrigin注解
也可以在Controller上添加該注解
5 路由出口
如果路由指向的頁面組件是同一個,那么路由出口顯示的頁面組件不會重新渲染
為使重新渲染,需要為路由出口定義一個唯一key值
6 springdoc openapi3 swagger3文件上傳
OpenAPI Specification - Version 3.1.0 | Swagger
#Considerations for File Uploads
In contrast with the 2.0 specification, input/output content in OpenAPI is described with the same semantics as any other schema type.
file
In contrast with the 3.0 specification, the keyword has no effect on the content-encoding of the schema. JSON Schema offers a keyword, which may be used to specify the for the schema. The keyword supports all encodings defined in RFC4648, including “base64” and “base64url”, as well as “quoted-printable” from RFC2045. The encoding specified by the keyword is independent of an encoding specified by the header in the request or response or metadata of a multipart body – when both are present, the encoding specified in the is applied first and then the encoding specified in the header.
format
contentEncoding
Content-Encoding
contentEncoding
contentEncoding
Content-Type
contentEncoding
Content-Type
JSON Schema also offers a keyword. However, when the media type is already specified by the Media Type Object’s key, or by the field of an Encoding Object, the keyword SHALL be ignored if present.
contentMediaType
contentType
contentMediaType
原來的寫法:
public void upload(@RequestParam("file") MultipartFile file){}
3.0版本的寫法:
public void upload(@RequestBody(content = @Content(mediaType = "multipart/form-data",schema = @Schema(type = "object"),schemaProperties = {@SchemaProperty(name = "file",schema = @Schema(type = "string",format = "binary"))})) MultipartFile file){}
7 連接mysql時出現[HY000][1130] null, message from server: “Host ‘‘ is not allowed報錯
出現這個問題先考慮端口3306是否開通,如果開通還出現報錯則開始下一步
解決這個問題有個很簡單的方法就是先進入mysql然后命令
use mysql
update user set host='%' where user='root';
然后通過命令
GRANT ALL ON *.* TO 'root'@'%';
FLUSH PRIVILEGES;
8 Nacos2.2.3 啟動成功但訪問空白
修改conf/application.properties文件
### If turn on auth system:
nacos.core.auth.enabled=true### Since 1.4.1, worked when nacos.core.auth.enabled=true and nacos.core.auth.enable.userAgentAuthWhite=false.
### The two properties is the white list for auth and used by identity the request from other server.
#用戶名
nacos.core.auth.server.identity.key=nacos
#密碼
nacos.core.auth.server.identity.value=nacos### The default token (Base64 String):
nacos.core.auth.plugin.nacos.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789
9 ERROR StatusLogger Log4j2 could not find a logging imple mentation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console…
添加依賴
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>2.0.13</version>
</dependency><!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>2.23.1</version>
</dependency>
問題源自于easyexcel
<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.3.4</version>
</dependency>
10 mapper.xml在pom文件中打包后找不到數據源配置了
mapper.xml文件在src/main/java/mapper/xml中,
在pom文件中添加了打包
<build><!-- 項目打包時會將java目錄中的*.xml文件也進行打包 --><resources><resource><directory>src/main/java</directory><includes><include>**/*.*</include></includes><filtering>false</filtering></resource></resources>
</build>
但是添加后,application.yml就變紅了,運行報錯
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
嘗試把resoources下的application.yml也打包到maven里,成功了。
<build><!-- 項目打包時會將java目錄中的*.xml文件也進行打包 --><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes><filtering>false</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**/*.yml</include></includes><filtering>false</filtering></resource></resources>
</build>
看來maven默認是都打包,但是如果我自定義的話就要全都定義了。
11 Refused to execute script from because its MIME type (‘text/html’) is not executable, and strict MIME type checking is enabled
每次打開前端,控制臺都會報錯,雖然不影響運行……
Refused to execute script from 'http://localhost:8080/something/index_bundle.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
某度某應都試過了,無果。在StackOverflow搜索了下,方案:
Your webpack config is malformed. So your devServer is returning the fallback html file instead of the bundle.
Hence why the script is served with the (‘text/html’) MIME type.
devServer: {historyApiFallback:{index:'/dist/index.html'},}
You probably meant something like this:
devServer: {historyApiFallback: true
}
前端的工程只找到了webpack.dev.conf.js
,果然有這一行,修改后,成功解決。
12 Nacos Whitelabel Error Page 503
Whitelabel Error Page
This application has no configured error view, so you are seeing this as a fallback.
Tue May 21 04:01:08 CST 2024
[9b38c884-1] There was an unexpected error (type=Service Unavailable, status=503).
用了負載均衡,控制臺一路綠燈過去的,頁面就是刷不出來,惱火。
查得,Nacos在2021版本起就刪除了Ribbon的包,需要引入springcloud-loadbalancer
所以,依賴應該這么引:
<!-- nacos -->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency><!-- loadbalancer -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
13 Mybatis-Plus代碼生成器
Spring Boot (v3.2.5)
mybatis-plus-generator (v3.5.6)
Mybatis-Plus代碼生成器配置
14 @RequestBody
如果是來自springframework的注解,則會根據定義自動封裝為Entity,
但如果是swagger的注解,沒有傳的參數就會自動填充為空。
比如User類的主鍵id設置了自增,前端傳入的json中沒有id值,如果是第一種,會自動封裝;如果是第二種,數據庫語句報錯。