Spring Boot Rest服务表格太大

时间:2016-04-26 18:05:35

标签: java spring rest spring-boot jetty

我正在使用Spring Boot 1.3.2,我需要公开REST Web服务。对于这个Web服务,我需要传递一个String(包含xml数据)作为参数。字符串的大小通常在2 MB到120 MB之间。问题是我不知道如何配置REST方法参数允许的最大大小,并且默认大小太小。由于其他依赖性,Spring配置必须位于java类中。这是我的配置类:

@Configuration
@ComponentScan("eu.buzea")
@EnableAutoConfiguration
@EnableNeo4jRepositories("eu.buzea.datamodel.repositories")
@EnableTransactionManagement
@SpringBootApplication
public class Application extends Neo4jConfiguration {

    public Application() {
        System.setProperty("username", <CONFIDENTIAL>);
        System.setProperty("password", <CONFIDENTIAL>);
    }

    @Override
    public Neo4jServer neo4jServer() {
        return new RemoteServer("http://localhost:7474");
    }

    @Override
    public SessionFactory getSessionFactory() {
        return new SessionFactory("eu.buzea.datamodel.entities");
    }

    @Override
    @Bean
    @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public Session getSession() throws Exception {
        return super.getSession();
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

这就是我的网络方法的样子:

@RequestMapping(method = RequestMethod.POST)
public boolean processing(@RequestParam(value = "xml", required = true) String xml) 

当我向方法发送一个String时,我收到以下错误:

java.lang.IllegalStateException: Form too large: 780963 > 200000
at org.eclipse.jetty.server.Request.extractFormParameters(Request.java:365) ~[jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.eclipse.jetty.server.Request.extractContentParameters(Request.java:303) ~[jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.eclipse.jetty.server.Request.extractParameters(Request.java:257) ~[jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.eclipse.jetty.server.Request.getParameter(Request.java:826) ~[jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:70) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) ~[jetty-servlet-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) ~[jetty-servlet-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585) [jetty-servlet-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577) [jetty-security-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) [jetty-servlet-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.eclipse.jetty.server.Server.handle(Server.java:499) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257) [jetty-server-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544) [jetty-io-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) [jetty-util-9.2.14.v20151106.jar:9.2.14.v20151106]
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) [jetty-util-9.2.14.v20151106.jar:9.2.14.v20151106]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_71]
2016-04-26 20:32:44 [qtp2021854618-21] WARN  org.eclipse.jetty.http.HttpParser - badMessage: java.lang.IllegalStateException: too much data after closed for HttpChannelOverHttp@4d208ff0{r=1,c=false,a=IDLE,uri=}

有谁能告诉我如何将(最大请求大小)最大格式内容大小设置为200 MB或无限?

更新 以下帖子提供了一个解决方案,以防您使用的是未安装在Spring Boot中的Jetty服务器:stackoverflow.com/questions/3861455/form-too-large-exception

我想设置 maxFormContentSize 参数,但我不确定它是Eclipse参数还是Spring参数。

P.S。我现在知道如何使用MultipartFile Upload解决问题。此外,还有另一种选择:在请求体中发送字符串/数据。它们都很好用,但我想知道是否可以在Spring或Eclipse中设置 maxFormContentSize

4 个答案:

答案 0 :(得分:3)

Spring Boot 1.4.0.M2有一个新属性@Bean public 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()); } } } }); } } }; } ,可用于配置它。里程碑可从https://repo.spring.io/libs-milestone获得。

如果您想坚持使用Spring Boot 1.3.x,您可以通过将以下bean添加到应用程序的配置中来实现相同的逻辑:

export

答案 1 :(得分:1)

处理大文件时,应使用multipart-file-upload。 Spring 3.0 MultipartFile upload

客户端应该以多部分格式向您发送数据 https://stackoverflow.com/a/27097798/5649869

答案 2 :(得分:0)

您是否尝试过增加HTTP POST maxPostSize?

application.properties文件中添加:

multipart.maxFileSize=20MB

文档:

http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/web/MultipartProperties.html

如果你想在代码中找到它,那就是文档:

Increase HTTP Post maxPostSize in Spring Boot

答案 3 :(得分:0)

对于没有 spring-boot 的简单 jetty-server-9.4.33.v20201020。它通过配置此系统属性来工作。码头使用 Integer.getInteger 函数获取 _maxFormContentSize 中静态字段 org.eclipse.jetty.server.handler.ContextHandler 的默认值。

public static void main(String[] args) {
    System.setProperty("org.eclipse.jetty.server.Request.maxFormContentSize", "20000000");   
    SpringApplication.run(Application.class, args);
}

对于 spring-boot-2.3.5,只有几个值被覆盖,如 org.springframework.boot.autoconfigure.web.ServerProperties.Jetty.maxHttpFormPostSize 可以配置

server.jetty.maxHttpFormPostSize=20000001B