Spring mvc 4.3.0 Request请求拿不到参数的问题解析

1、问题描述:
最近在使用Spring、Springmvc 新的 4.3.0 版本搭建开发框架;
jdk使用了1.8的版本,Tomcat服务器使用了apache-tomcat-6.0.32.tar.gz 版本;
根据以上的环境,通过如下的访问方式:http://man.notice.jd.com/worker/once?quartzBeanId=1000;
后台Springmvc 的 Controller 处理方法在获取 quartzBeanId 时值为 null

    @RequestMapping("/once")
    public String once(String quartzBeanId) {

        log.info("quartzBeanId=" + quartzBeanId);

        return "forward:/worker/list";
    }

2、问题跟踪
通过跟踪源代码,发现在Spring Mvc 源码
org.springframework.web.multipart.support.MultipartResolutionDelegate#resolveMultipartArgument
这里写图片描述

org.springframework.web.multipart.support.MultipartResolutionDelegate#isPartCollection
该方法返回了 true

    private static boolean isPartCollection(MethodParameter methodParam) {
        return (servletPartClass == getCollectionParameterType(methodParam));
    }

为什么会返回 true ?

通过断点发现:servletPartClass = null

看 org.springframework.web.multipart.support.MultipartResolutionDelegate 源代码如下:
servletPartClass 是在静态块里面被初始化的,但是通过 try …… catch 抓住了异常情况,问题就出在这里面,javax.servlet.http.Part 这个类在 apache-tomcat-6.0.32.tar.gz 版本的 servlet-api.jar 中不存在!!!

public abstract class MultipartResolutionDelegate {

    public static final Object UNRESOLVABLE = new Object();


    private static Class<?> servletPartClass = null;

    static {
        try {
            servletPartClass = ClassUtils.forName("javax.servlet.http.Part",
                    MultipartResolutionDelegate.class.getClassLoader());
        }
        catch (ClassNotFoundException ex) {
            // Servlet 3.0 javax.servlet.http.Part type not available -
            // Part references simply not supported then.
        }
    }

}

以下是Tomcat7 源码解压的截图。
这里写图片描述

3、问题解决方案
将Tomcat的版本升级到 7.0 以上的版本之后,这个问题就解决了!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Knife4j v4.3.0中,如果请求参数的类中包含MultipartFile类型的属性,可以通过以下步骤来显示MultipartFile类型的属性: 1. 确保你的项目中已经引入了Knife4j的依赖。 2. 在你的请求参数类中,使用`@ApiModelProperty`注解来标注MultipartFile类型的属性,并设置`dataType`属性为`"file"`。 ```java import io.swagger.annotations.ApiModelProperty; import org.springframework.web.multipart.MultipartFile; public class YourRequestClass { @ApiModelProperty(dataType = "file") private MultipartFile file; // 其他属性和方法... } ``` 3. 在Knife4j的配置类中,使用`globalRequestParameters`方法来全局配置请求参数的显示。 ```java import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ParameterBuilder; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.schema.ModelRef; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 @EnableKnife4j public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("your.package.name")) .build() .globalRequestParameters(Collections.singletonList( new ParameterBuilder() .name("file") .description("文件") .modelRef(new ModelRef("file")) .parameterType("form") .required(false) .build() )); } } ``` 通过以上步骤,你就可以在Knife4j中显示MultipartFile类型的属性了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值