Jersey/Grizzly POST fails for large URI

2019-08-07 19:48发布

问题:

Relevant: this and that

I'm developing a POST webservice (jersey/grizzly, for research purposes only) which should be able to handle large URIs. But if a URI exceeds above 8000 characters I get a 400 Bad Request Exception with no further explanation. I debugged/tracked it down to the grizzly maxHttpHeaderSize attribute. I tried to set this attribute but failed. Here is how I start the server and the request:

GrizzlyServerFactory.createHttpServer(BASE_URI, new PackagesResourceConfig("org.test"));

JSONObject s = new JSONObject(webResource.queryParams(queryParams).post(String.class));

Thank you for your help! Cheers, Daniel

回答1:

The problem is that GrizzlyServerFactory returns already started HttpServer, that's why you can not reconfigure it on the fly. Here [1] I've created a copy of the GrizzlyServerFactory , which doesn't start HttpServer, so the code like:

    HttpServer httpServer = GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
    httpServer.getListener("grizzly").setMaxHttpHeaderSize(Integer.MAX_VALUE);

    // don't forget to start the server explicitly
    httpServer.start();

Hope that will help.

[1] https://github.com/oleksiys/samples/blob/master/jersey-grizzly2-ext/src/main/java/com/sun/jersey/api/container/grizzly2/ext/GrizzlyServerFactory.java



回答2:

Have You tried with shorter URI? Probably Your server is unable to handle so long URI.

You can read more about it here: What is the maximum length of a URL in different browsers?

Why don't You put request data from URL to request body?



回答3:

You can create the grizzly http server without starting it now

public static HttpServer createHttpServer(
     final URI uri, 
     final ResourceConfig configuration, 
     final boolean start)

So basically you create it normally passing false as 3rd argument.

When you get the HttpServer object back you can httpServer.getListener("grizzly").setMaxHttpHeaderSize(Integer.MAX_VALUE); As was said before and don't forget to start the server manually.