1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SparkでPOSTデータの最大個数と最大数を変更する方法

Last updated at Posted at 2018-02-19

Jettyでフォームの値の最大値は、デフォルトで200KBになっている。
SparkはJettyを利用しているので、これに準ずる。

Jetty単体の場合は色々な設定方法があるようだが、Sparkの場合は結構めんどくさい。

詳細は省くが、やり方は下記のとおり。

public class Main {
  public static void main(String[] args) {
    EmbeddedServers.add(EmbeddedServers.Identifiers.JETTY, new EmbeddedJettyFactory(new MyJettyServerFactory()));

    get("/", () -> "Hello Spark!");
  }
}

class MyJettyServerFactory implements JettyServerFactory {
    /**
     * Creates a Jetty server.
     *
     * @param maxThreads          maxThreads
     * @param minThreads          minThreads
     * @param threadTimeoutMillis threadTimeoutMillis
     * @return a new jetty server instance
     */
	@Override
    public Server create(int maxThreads, int minThreads, int threadTimeoutMillis) {
        Server server;

        if (maxThreads > 0) {
            int max = maxThreads;
            int min = (minThreads > 0) ? minThreads : 8;
            int idleTimeout = (threadTimeoutMillis > 0) ? threadTimeoutMillis : 60000;

            server = new Server(new QueuedThreadPool(max, min, idleTimeout));
        } else {
            server = new Server();
        }

        server.setAttribute("org.eclipse.jetty.server.Request.maxFormKeys", 20000); // POSTデータの最大個数
        server.setAttribute("org.eclipse.jetty.server.Request.maxFormContentSize", 5000000); // POSTデータの最大値

        return server;
    }

    /**
     * Creates a Jetty server with supplied thread pool
     * @param threadPool thread pool
     * @return a new jetty server instance
     */
	@Override
    public Server create(ThreadPool threadPool) {
        return threadPool != null ? new Server(threadPool) : new Server();
    }
}

ちなみにプログラムでフォームの値を取得しない限り、エラーとはならない。
これって仕様的にどうなんだろう。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?