SpringBoot http post請求數(shù)據(jù)大小設(shè)置操作
背景:
使用http post請求方式的接口,使用request.getParameter('XXX');的方法獲取參數(shù)的值,當(dāng)數(shù)據(jù)量超過幾百k的時候,接口接收不到數(shù)據(jù)或者接收為null。
@RequestMapping(value = '/rcv',method = RequestMethod.POST) public ResInfo<String> pullApi(HttpServletRequest request) { String channel = request.getParameter('channel'); }
在application.properties里添加:
spring.http.multipart.max-file-size=-1
spring.http.multipart.max-request-size=-1
默認值:
private String maxFileSize = '1MB'; private String maxRequestSize = '10MB';
這個設(shè)置是大小不限制,主要是這個設(shè)置。
server.tomcat.max-http-post-size=-1
補充知識:spring boot post請求數(shù)據(jù)太大接收不到參數(shù)(參數(shù)為空)報400錯誤。
spirng boot發(fā)送的請求,有的參數(shù)數(shù)據(jù)量大,有的參數(shù)數(shù)據(jù)量小。數(shù)據(jù)量大的發(fā)送不到后端,報400錯誤。
首先說以jetty為內(nèi)嵌服務(wù)器的時候:
1、修改yml文件或者properties文件,
server.max-http-post-size=200*1024*1024,設(shè)置為200m,總夠了吧,可以按照量設(shè)置小一點。
Spring Boot 1.4.0.M2之后就可以使用這個屬性。適合于不修改代碼,快速重新部署。
2、在java代碼里面實現(xiàn):
@Beanpublic EmbeddedServletContainerCustomizer jettyCustomizer() { return new EmbeddedServletContainerCustomizer() { @Override public void customize(ConfigurableEmbeddedServletContainer container) { if (container instanceof JettyEmbeddedServletContainerFactory) { ((JettyEmbeddedServletContainerFactory) container) .addServerCustomizers(new JettyServerCustomizer() { @Override public void customize(Server server) { setHandlerMaxHttpPostSize(200 * 1024 * 1024, server.getHandlers()); } private void setHandlerMaxHttpPostSize(int maxHttpPostSize, Handler... handlers) { for (Handler handler : handlers) { if (handler instanceof ContextHandler) {((ContextHandler) handler) .setMaxFormContentSize(maxHttpPostSize); } else if (handler instanceof HandlerWrapper) {setHandlerMaxHttpPostSize(maxHttpPostSize, ((HandlerWrapper) handler).getHandler()); } else if (handler instanceof HandlerCollection) {setHandlerMaxHttpPostSize(maxHttpPostSize, ((HandlerCollection) handler).getHandlers()); } } } }); } } };}
Spring Boot 1.3.x之后可以使用這個功能。
3、設(shè)置接收數(shù)據(jù)在 request body里面。上面兩種情況適合于數(shù)據(jù)不放在request body里面的。
tomcat應(yīng)該是類似的。
參考https://stackoverflow.com/questions/36872540/spring-boot-rest-service-form-too-large
以上這篇SpringBoot http post請求數(shù)據(jù)大小設(shè)置操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. XML入門的常見問題(二)2. jsp實現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫的方法3. asp.net core項目授權(quán)流程詳解4. XML入門的常見問題(三)5. CSS3實現(xiàn)動態(tài)翻牌效果 仿百度貼吧3D翻牌一次動畫特效6. .NET使用YARP通過編碼方式配置域名轉(zhuǎn)發(fā)實現(xiàn)反向代理7. ASP動態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗分享8. 開發(fā)效率翻倍的Web API使用技巧9. jsp cookie+session實現(xiàn)簡易自動登錄10. 怎樣才能用js生成xmldom對象,并且在firefox中也實現(xiàn)xml數(shù)據(jù)島?
