广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Springboot上传文件时提示405问题及排坑过程
  • 683
分享到

Springboot上传文件时提示405问题及排坑过程

2024-04-02 19:04:59 683人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录SpringBoot上传文件时提示405解决方案1解决方案2springboot使用过程中遇到的一些问题异常一异常二:Mysql连接报错异常三:整合Druid密码解密失败Spri

Springboot上传文件时提示405

问题描述:上传文件时请求不通,状态码返回405,如下图:

 问题分析:405 Method Not Allowed,请求行中指定的请求方法不能被用于请求相应的资源。该响应必须返回一个Allow 头信息用以表示出当前资源能够接受的请求方法的列表。简单说就是请求方法不支持,这样就找到了一个解决方向。(以下分析了3种返回该错误的情况)

解决方案1

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

解决方案2

发现接口确实支持POST请求,那么问题就不是这么明显了。因为该接口是用于文件上传,所以问题应该是在这里。由于Springboot默认的文件上传大小为1MB,自己再看发现文件大小超过了限制(上传的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:修改文件大小配置后,发现还是报405,这就懵了,继续看后台日志:

明白了,发现后端使用@RequestParam(“file”) 注解标注的MultipartFile参数没有获取到文件,对比前端请求参数名发现不一致造成的。

问题解决。 

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)

分析:密码使用明文时,能正常连接数据库,查询到表数据。分析是密码没有成功解密。

pom添加的Druid依赖如下:

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

Druid解密是在ConfigFilter中init中进行的,断点跟踪如下图,配置中的config.decrypt,config.decrypt.key是连在一起的,没有分开,所以decrypt=false,没有对密码进行解密操作

 检查配置,数据库配置如下图

原因:connect-properties是druid-spring-boot-starter包下的配置参数,没有对properties进行解析处理,换成Druid报下的配置参数connection-properties。properties参数会进行按 ; 进行分隔处理,获取到具体的配置信息。

解决方案:换成connection-properties即可

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: Springboot上传文件时提示405问题及排坑过程

本文链接: https://www.lsjlt.com/news/153171.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Springboot上传文件时提示405问题及排坑过程
    目录Springboot上传文件时提示405解决方案1解决方案2Springboot使用过程中遇到的一些问题异常一异常二:Mysql连接报错异常三:整合Druid密码解密失败Spri...
    99+
    2022-11-13
  • 微信小程序中如何解决show内容展示以及上传文件编码问题
    这篇文章主要介绍了微信小程序中如何解决show内容展示以及上传文件编码问题,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。show内容展示尝试...
    99+
    2022-10-19
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作