Spring 引导 POST 参数大小限制

Spring Boot POST parameter size limit

我似乎在某处与限制器对接。我的 Spring-Boot REST 端点 (POST) 参数 (surveyResults) 之一正在寻找 JSON:

的字符串

    private static final String SURVEY_RESULTS_ENDPOINT = "/survey/results";

    @PostMapping(
        value = SURVEY_RESULTS_ENDPOINT, 
        produces = { "application/hal+json", "application/json" }
    )   
    @ApiOperation(value = "Save one survey results")
    public Resource<SurveyResult> createSurveyResults(

            @ApiParam(value = "who/what process created this record", required = true) @Valid 
                @RequestParam(value = "recordCreatedBy", required = true) String createdBy,

            @ApiParam(value = "was an issue identified", required = true) 
                @RequestParam(value = "hadFailure", required = true) Boolean hadFailure,

            @ApiParam(value = "JSON representation of the results", required = true) 
                @RequestParam(value = "surveyResults", required = true) String surveyResult

    ) ...

如果我 post 使用大约 1500 个字符,它就可以工作。就在那上面的某个地方,它将失败并出现 HTTP 400 错误 bad request。加上其他参数,整个payload不到2K

我刚从 Wildfly 转移到新的服务器设置。我的公司正在采用对云服务器的持续部署,因此我对这个新的负载平衡服务器没有太多的控制权和可见性。服务器是 "server": "openresty/1.13.6.2" - 知道我的限制是 运行 吗?

请使用@RequestBody代替@RequestParam

@RequestBody 注释将 HTTP 请求的主体映射到一个对象。 @RequestParam 映射请求中的请求参数,在URL中,不在正文中。

大多数浏览器对请求参数中支持的字符数有限制,您只需达到该限制即可。

我建议创建一个如下所示的 POJO

public class Body {
   private String createdBy; 
   private Boolean hadFailure;  
   private String surveyResult;

// getters and setters
}

现在你的控制器会更简单

@PostMapping(
        value = SURVEY_RESULTS_ENDPOINT, 
        produces = { "application/hal+json", "application/json" }
    )   
public Resource<SurveyResult> createSurveyResults(@RequestBody Body body) {

}

无论您 post 在哪里,您现在都必须 post 一个 JSON (Content-Type = application/json),如下所示

{ "createdBy" : "foo", "hadFailure" : false, "surveyResult" : "the foo bar"}