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

 更新时间:2022年07月01日 10:43:47   作者:zhangjp505  
这篇文章主要介绍了Springboot上传文件时提示405问题及排坑过程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

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即可

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • cmd使用javac和java及注意事项

    cmd使用javac和java及注意事项

    这篇文章主要介绍了cmd使用javac和java及注意事项,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-12-12
  • SpringBoot如何获取当前操作用户的id/信息

    SpringBoot如何获取当前操作用户的id/信息

    在一般性的基设需求中,有需要获取当前用户操作记录的情况,也就是说我们需要记录当前用户的信息,如:id、昵称、账号等信息,这篇文章主要介绍了SpringBoot获取当前操作用户的id/信息,需要的朋友可以参考下
    2023-10-10
  • Java毕业设计实战之医院心理咨询问诊系统的实现

    Java毕业设计实战之医院心理咨询问诊系统的实现

    这是一个使用了java+Spring+Maven+mybatis+Vue+mysql开发的医院心理咨询问诊系统,是一个毕业设计的实战练习,具有心理咨询问诊该有的所有功能,感兴趣的朋友快来看看吧
    2022-01-01
  • Lucene词向量索引文件构建源码解析

    Lucene词向量索引文件构建源码解析

    这篇文章主要为大家介绍了Lucene词向量索引文件构建源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • 基于Java的Socket多客户端Client-Server聊天程序的实现

    基于Java的Socket多客户端Client-Server聊天程序的实现

    这篇文章主要介绍了基于Java的Socket多客户端Client-Server聊天程序的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • Servlet简单实现登录功能

    Servlet简单实现登录功能

    这篇文章主要为大家详细介绍了Servlet简单实现登录功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-03-03
  • mybatis中几种typeHandler的定义使用详解

    mybatis中几种typeHandler的定义使用详解

    本文主要介绍了mybatis中几种typeHandler的定义使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-12-12
  • Java中静态代理的使用与原理详解

    Java中静态代理的使用与原理详解

    这篇文章主要介绍了Java中静态代理的使用与原理详解,代理模式是为一个对象提供一个替身,以控制对这个对象的访问,即通过代理对象访问目标对象.这样做的好处是:可以在目标对象实现的基础上,增强额外的功能操作,即扩展目标对象的功能,需要的朋友可以参考下
    2023-09-09
  • Springmvc应用Mongodb分页实现

    Springmvc应用Mongodb分页实现

    这篇文章主要为大家详细介绍了Springmvc应用Mongodb分页实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11
  • JDK动态代理过程原理及手写实现详解

    JDK动态代理过程原理及手写实现详解

    这篇文章主要为大家介绍了JDK动态代理过程原理及手写实现详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09

最新评论