欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-15 12:59:21
...

转发请注明出处: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.getMaxHttpHeaderSize,而ServerProperties类是@ConfigurationProperties,所以我们只需要在application.yml中配置即可:

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

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