【问题标题】:Received Field is Empty when the Size is Big in Spring Boot Post MethodSpring Boot Post方法中大小较大时接收字段为空
【发布时间】:2018-10-09 00:36:54
【问题描述】:

我尝试通过POST方式向SpringBoot webserver发送一个字符串,当字符串较小(200k)时,可以在控制器端接收,而当字符串较大(2M)时,控制器字段为空,没有springBoot 日志中的任何错误消息。

控制器代码sn-p为:

@PostMapping(value = "/func")
public ResponseMessage func(@RequestParam(value = "message", defaultValue = "") String message) {
    if(StringUtils.isEmpty(message))    {
        // alert
    }
}

发件人的代码sn-p是:

PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
// Create socket configuration
SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true)
        .setSoTimeout(180000).setSoReuseAddress(true).setTcpNoDelay(true).build();

// Configure the connection manager to use socket configuration either
// by default or for a specific host.
connManager.setDefaultSocketConfig(socketConfig);
// Validate connections after 1 minute of inactivity
connManager.setValidateAfterInactivity(180000);
connManager.setMaxTotal(100);
connManager.setDefaultMaxPerRoute(20);

// Create global request configuration
RequestConfig defaultRequestConfig = RequestConfig.custom()
        .setCookieSpec(CookieSpecs.DEFAULT).setExpectContinueEnabled(true)
        .setConnectTimeout(180000).setSocketTimeout(180000)
        .setConnectionRequestTimeout(180000).build();

CloseableHttpClient hc = HttpClients.custom().setConnectionManager(connManager)
        .setDefaultRequestConfig(defaultRequestConfig).setDefaultSocketConfig(socketConfig)
        .build();


String encodedMsg = new String(Files.readAllBytes(Paths.get(args[0])), UTF_8);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(Lists.newArrayList(new BasicNameValuePair("message", encodedMsg)), UTF_8);
HttpUriRequest httpPost = HttpUtils.post("http://url/func", entity);

try (CloseableHttpResponse response = hc.execute(httpPost)) {
    // ...
}

我尝试在 application.properties 中添加以下 request-body-size 参数,但它似乎不起作用。有什么建议?谢谢。

multipart.maxRequestSize=30MB
multipart.maxFileSize=30MB
spring.http.multipart.maxFileSize=30Mb
spring.http.multipart.maxRequestSize=30Mb

SpringBoot:1.5.10.RELEASE

春季:4.3.14.RELEASE

【问题讨论】:

    标签: spring tomcat spring-boot


    【解决方案1】:

    2MB 是 tomcat 中 Post 请求参数的默认最大大小。 假设您使用的是 tomcat,可以通过以下方式关闭最大帖子大小限制 设置

    server.tomcat.max-http-post-size=-1
    

    【讨论】:

    • 是的,我正在使用 spring boot 和嵌入式 tomcat。所以我只是把上面的参数放在 application.properties 中?
    • 是的,它现在正在工作。作为参考,有关嵌入式 tomcat 的参数可以在这里找到:docs.spring.io/spring-boot/docs/current/reference/html/…
    猜你喜欢
    • 2019-05-23
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 2015-03-26
    • 2016-12-08
    相关资源
    最近更新 更多