Springboot 2.x + Jetty 9.x java.lang.IllegalStateException: Form too large

转发请注明出处:https://blog.csdn.net/LvShuiLanTian/article/details/88647542

查看异常处代码(org.eclipse.jetty.server.Request)

public void extractFormParameters(MultiMap<String> params)
    {
        try
        {
            int maxFormContentSize = -1;
            int maxFormKeys = -1;

            if (_context != null)
            {
                maxFormContentSize = _context.getContextHandler().getMaxFormContentSize();
                maxFormKeys = _context.getContextHandler().getMaxFormKeys();
            }

            ...

            int contentLength = getContentLength();
            if (contentLength > maxFormContentSize && maxFormContentSize > 0)
            {
                throw new IllegalStateException("Form too large: " + contentLength + " > " + maxFormContentSize);
            }
            ...
        }
        catch (IOException e)
        {
            LOG.debug(e);
            throw new RuntimeIOException(e);
        }
    }

因为contentLength > maxFormContentSize 导致, 而maxFormContentSize可从ContextHandler获取,所以只要我们将ContextHandler的maxFormContentSize设置大一些即可

源码查看JettyWebServerFactoryCustomizer类源代码

private void customizeMaxHttpPostSize(ConfigurableJettyWebServerFactory factory,
			int maxHttpPostSize) {
		factory.addServerCustomizers(new JettyServerCustomizer() {

			@Override
			public void customize(Server server) {
				setHandlerMaxHttpPostSize(maxHttpPostSize, 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());
					}
				}
			}

		});
	}

我们发现((ContextHandler) handler).setMaxFormContentSize(maxHttpPostSize);,对maxFormContentSize进行了赋值,继续查看customizeMaxHttpPostSize调用方为同一个类下的customize方法

public void customize(ConfigurableJettyWebServerFactory factory) {
		ServerProperties properties = this.serverProperties;
		ServerProperties.Jetty jettyProperties = properties.getJetty();
		factory.setUseForwardHeaders(
				getOrDeduceUseForwardHeaders(properties, this.environment));
		PropertyMapper propertyMapper = PropertyMapper.get();
		propertyMapper.from(jettyProperties::getAcceptors).whenNonNull()
				.to(factory::setAcceptors);
		propertyMapper.from(jettyProperties::getSelectors).whenNonNull()
				.to(factory::setSelectors);
		propertyMapper.from(properties::getMaxHttpHeaderSize).when(this::isPositive)
				.to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory,
						maxHttpHeaderSize));
		propertyMapper.from(jettyProperties::getMaxHttpPostSize).when(this::isPositive)
				.to((maxHttpPostSize) -> customizeMaxHttpPostSize(factory,
						maxHttpPostSize));
		propertyMapper.from(properties::getConnectionTimeout).whenNonNull()
				.to((connectionTimeout) -> customizeConnectionTimeout(factory,
						connectionTimeout));
		propertyMapper.from(jettyProperties::getAccesslog)
				.when(ServerProperties.Jetty.Accesslog::isEnabled)
				.to((accesslog) -> customizeAccessLog(factory, accesslog));
	}

通过上述方法,可以发现customizeMaxHttpPostSize方法的maxHttpPostSize参数源自于ServerProperties.Jetty.getMaxHttpPostSize,而ServerProperties类是@ConfigurationProperties,所以我们只需要在application.yml中配置即可:

server:
  jetty:
    max-http-post-size: 2000000

转发请注明出处:https://blog.csdn.net/LvShuiLanTian/article/details/88647542

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值