欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot上傳文件時(shí)提示405問(wèn)題及排坑過(guò)程

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

Springboot上傳文件時(shí)提示405

問(wèn)題描述:上傳文件時(shí)請(qǐng)求不通,狀態(tài)碼返回405,如下圖:

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

解決方案1

看下接口是否支持請(qǐng)求的方式,文件上傳使用的POST方法,看下接口是否支持。后臺(tái)日志如下:

解決方案2

發(fā)現(xiàn)接口確實(shí)支持POST請(qǐng)求,那么問(wèn)題就不是這么明顯了。因?yàn)樵摻涌谑怯糜谖募蟼鳎詥?wèn)題應(yīng)該是在這里。由于Springboot默認(rèn)的文件上傳大小為1MB,自己再看發(fā)現(xiàn)文件大小超過(guò)了限制(上傳的7.13MB),后臺(tái)日志如下:

修改Springboot配置文件:

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

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

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

問(wèn)題解決。 

Springboot使用過(guò)程中遇到的一些問(wèn)題

僅記錄個(gè)人遇到的一些問(wèn)題及原因,和解決方案。

異常一

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

原因:配置項(xiàng)目名稱(chēng)配置錯(cuò)誤

錯(cuò)誤配置:

server:
  context-path: admin

正確配置:

server:
  context-path: /admin

異常二:Mysql連接報(bào)錯(cuò)

Could not create connection to database server

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

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

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

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

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

pom添加的Druid依賴(lài)如下:

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

Druid解密是在ConfigFilter中init中進(jìn)行的,斷點(diǎn)跟蹤如下圖,配置中的config.decrypt,config.decrypt.key是連在一起的,沒(méi)有分開(kāi),所以decrypt=false,沒(méi)有對(duì)密碼進(jìn)行解密操作

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

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

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

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

相關(guān)文章

  • cmd使用javac和java及注意事項(xiàng)

    cmd使用javac和java及注意事項(xiàng)

    這篇文章主要介紹了cmd使用javac和java及注意事項(xiàng),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2021-12-12
  • SpringBoot如何獲取當(dāng)前操作用戶的id/信息

    SpringBoot如何獲取當(dāng)前操作用戶的id/信息

    在一般性的基設(shè)需求中,有需要獲取當(dāng)前用戶操作記錄的情況,也就是說(shuō)我們需要記錄當(dāng)前用戶的信息,如:id、昵稱(chēng)、賬號(hào)等信息,這篇文章主要介紹了SpringBoot獲取當(dāng)前操作用戶的id/信息,需要的朋友可以參考下
    2023-10-10
  • Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之醫(yī)院心理咨詢(xún)問(wèn)診系統(tǒng)的實(shí)現(xiàn)

    Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之醫(yī)院心理咨詢(xún)問(wèn)診系統(tǒng)的實(shí)現(xiàn)

    這是一個(gè)使用了java+Spring+Maven+mybatis+Vue+mysql開(kāi)發(fā)的醫(yī)院心理咨詢(xún)問(wèn)診系統(tǒng),是一個(gè)畢業(yè)設(shè)計(jì)的實(shí)戰(zhàn)練習(xí),具有心理咨詢(xún)問(wèn)診該有的所有功能,感興趣的朋友快來(lái)看看吧
    2022-01-01
  • Lucene詞向量索引文件構(gòu)建源碼解析

    Lucene詞向量索引文件構(gòu)建源碼解析

    這篇文章主要為大家介紹了Lucene詞向量索引文件構(gòu)建源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • 基于Java的Socket多客戶端Client-Server聊天程序的實(shí)現(xiàn)

    基于Java的Socket多客戶端Client-Server聊天程序的實(shí)現(xiàn)

    這篇文章主要介紹了基于Java的Socket多客戶端Client-Server聊天程序的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Servlet簡(jiǎn)單實(shí)現(xiàn)登錄功能

    Servlet簡(jiǎn)單實(shí)現(xiàn)登錄功能

    這篇文章主要為大家詳細(xì)介紹了Servlet簡(jiǎn)單實(shí)現(xiàn)登錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • mybatis中幾種typeHandler的定義使用詳解

    mybatis中幾種typeHandler的定義使用詳解

    本文主要介紹了mybatis中幾種typeHandler的定義使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • Java中靜態(tài)代理的使用與原理詳解

    Java中靜態(tài)代理的使用與原理詳解

    這篇文章主要介紹了Java中靜態(tài)代理的使用與原理詳解,代理模式是為一個(gè)對(duì)象提供一個(gè)替身,以控制對(duì)這個(gè)對(duì)象的訪問(wèn),即通過(guò)代理對(duì)象訪問(wèn)目標(biāo)對(duì)象.這樣做的好處是:可以在目標(biāo)對(duì)象實(shí)現(xiàn)的基礎(chǔ)上,增強(qiáng)額外的功能操作,即擴(kuò)展目標(biāo)對(duì)象的功能,需要的朋友可以參考下
    2023-09-09
  • Springmvc應(yīng)用Mongodb分頁(yè)實(shí)現(xiàn)

    Springmvc應(yīng)用Mongodb分頁(yè)實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了Springmvc應(yīng)用Mongodb分頁(yè)實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • JDK動(dòng)態(tài)代理過(guò)程原理及手寫(xiě)實(shí)現(xiàn)詳解

    JDK動(dòng)態(tài)代理過(guò)程原理及手寫(xiě)實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了JDK動(dòng)態(tài)代理過(guò)程原理及手寫(xiě)實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09

最新評(píng)論