最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot上傳文件時提示405問題及排坑過程

 更新時間:2022年07月01日 10:43:47   作者:zhangjp505  
這篇文章主要介紹了Springboot上傳文件時提示405問題及排坑過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Springboot上傳文件時提示405

問題描述:上傳文件時請求不通,狀態(tài)碼返回405,如下圖:

 問題分析:405 Method Not Allowed,請求行中指定的請求方法不能被用于請求相應的資源。該響應必須返回一個Allow 頭信息用以表示出當前資源能夠接受的請求方法的列表。簡單說就是請求方法不支持,這樣就找到了一個解決方向。(以下分析了3種返回該錯誤的情況)

解決方案1

看下接口是否支持請求的方式,文件上傳使用的POST方法,看下接口是否支持。后臺日志如下:

解決方案2

發(fā)現(xiàn)接口確實支持POST請求,那么問題就不是這么明顯了。因為該接口是用于文件上傳,所以問題應該是在這里。由于Springboot默認的文件上傳大小為1MB,自己再看發(fā)現(xiàn)文件大小超過了限制(上傳的7.13MB),后臺日志如下:

修改Springboot配置文件:

# 找到Springboot的application.properties配置文件,新增以下配置
spring.servlet.multipart.enabled=true #是否啟用http上傳處理
spring.servlet.multipart.max-request-size=10MB #最大請求文件的大小
spring.servlet.multipart.max-file-size=10MB #設置單個文件最大長度

解決方案3:修改文件大小配置后,發(fā)現(xiàn)還是報405,這就懵了,繼續(xù)看后臺日志:

明白了,發(fā)現(xiàn)后端使用@RequestParam(“file”) 注解標注的MultipartFile參數(shù)沒有獲取到文件,對比前端請求參數(shù)名發(fā)現(xiàn)不一致造成的。

問題解決。 

Springboot使用過程中遇到的一些問題

僅記錄個人遇到的一些問題及原因,和解決方案。

異常一

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: ContextPath must start with '/' and not end with '/'
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at com.pjx.Application.main(Application.java:15) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: ContextPath must start with '/' and not end with '/'
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:199) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:162) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    ... 8 common frames omitted
Caused by: java.lang.IllegalArgumentException: ContextPath must start with '/' and not end with '/'
    at org.springframework.boot.context.embedded.AbstractConfigurableEmbeddedServletContainer.checkContextPath(AbstractConfigurableEmbeddedServletContainer.java:132) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.context.embedded.AbstractConfigurableEmbeddedServletContainer.setContextPath(AbstractConfigurableEmbeddedServletContainer.java:120) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.autoconfigure.web.ServerProperties.customize(ServerProperties.java:198) ~[spring-boot-autoconfigure-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor.postProcessBeforeInitialization(EmbeddedServletContainerCustomizerBeanPostProcessor.java:73) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor.postProcessBeforeInitialization(EmbeddedServletContainerCustomizerBeanPostProcessor.java:59) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    ... 16 common frames omitted

原因:配置項目名稱配置錯誤

錯誤配置:

server:
  context-path: admin

正確配置:

server:
  context-path: /admin

異常二:Mysql連接報錯

Could not create connection to database server

原因:MySql版本(8.0.28)和MySql驅動版本(5.1.45)不匹配,驅動版本換成8.0+就ok了。

使用:select version() from DUAL查詢mysql版本。

異常三:整合Druid密碼解密失敗

異常棧:Caused by: com.mysql.cj.exceptions.CJException: Access denied for user 'root'@'localhost' (using password: YES)

分析:密碼使用明文時,能正常連接數(shù)據(jù)庫,查詢到表數(shù)據(jù)。分析是密碼沒有成功解密。

pom添加的Druid依賴如下:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
</dependency>

Druid解密是在ConfigFilter中init中進行的,斷點跟蹤如下圖,配置中的config.decrypt,config.decrypt.key是連在一起的,沒有分開,所以decrypt=false,沒有對密碼進行解密操作

 檢查配置,數(shù)據(jù)庫配置如下圖

原因:connect-properties是druid-spring-boot-starter包下的配置參數(shù),沒有對properties進行解析處理,換成Druid報下的配置參數(shù)connection-properties。properties參數(shù)會進行按 ; 進行分隔處理,獲取到具體的配置信息。

解決方案:換成connection-properties即可

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論

会宁县| 台东市| 于都县| 昭平县| 塔城市| 桂平市| 平遥县| 孝昌县| 施秉县| 福清市| 大新县| 周至县| 陇南市| 额敏县| 灵石县| 甘孜| 大余县| 阳城县| 长垣县| 辽宁省| 得荣县| 武胜县| 昔阳县| 应用必备| 昌都县| 当涂县| 阳新县| 藁城市| 镇江市| 微博| 唐河县| 衡阳市| 营山县| 同心县| 常山县| 兴海县| 炉霍县| 阳曲县| 宣恩县| 平武县| 塔河县|