在Spring Boot Post方法中,当Size大时,接收字段为空

时间:2018-04-28 08:55:54

标签: spring tomcat spring-boot

我尝试通过POST方法将字符串发送到SpringBoot网络服务器,当字符串很小(200k)时,它可以在控制器端接收,而当字符串相对较大(2M)时,控制器的字段为空而没有springBoot日志中的任何错误消息。

控制器代码段为:

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

并且发件人的代码段是:

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中添加以下请求体大小的参数,但似乎无法正常工作。有什么建议?感谢。

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

1 个答案:

答案 0 :(得分:1)

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

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