制作独立的grizzly2 JAX-RS接受URL中的编码URL?

时间:2018-04-20 14:11:41

标签: java rest jax-rs grizzly

我正在尝试将可能泄漏的库封装在自己的独立JVM中并使用REST调用它。我有一个Java EE 7网络应用程序可以做到这一点,但我想要一个独立的版本。为此,我使用

创建了一个小型Maven项目
mvn archetype:generate -DarchetypeGroupId=org.glassfish.jersey.archetypes \
 -DarchetypeArtifactId=jersey-quickstart-grizzly2 -DarchetypeVersion=2.27

这给了我一个关于这个Main的小项目。

public class Main {
    // Base URI the Grizzly HTTP server will listen on
    public static final String BASE_URI = "http://localhost:8080/verapdf/";

    /**
     * Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
     * @return Grizzly HTTP server.
     */
    public static HttpServer startServer() {
        // create a resource config that scans for JAX-RS resources and providers
        // in dk.kb.dpa.verapdf package
        final ResourceConfig rc = new ResourceConfig().packages("dk.kb.dpa.verapdf");

        // create and start a new instance of grizzly http server
        // exposing the Jersey application at BASE_URI
        return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
    }

    /**
     * Main method.
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        final HttpServer server = startServer();
        System.out.println(String.format("Jersey app started with WADL available at "
                + "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
        System.in.read();
        server.stop();
    }
}

我添加了自己的小资源,如:

.
.
.
@Path("validate")
public class JAXRSValidator {

    @GET
    @Path("{url}")
    @Produces(MediaType.APPLICATION_XML)
    public String validate(@PathParam("url") String url) throws Exception 
.
.
.

到目前为止,我可以调用http://localhost:8080/verapdf/validate/123并在url设置为123的方法中点击断点。但是,我想在这里有一个真正的URL(这将是绝对的,包括斜杠和hashmarks),这会给出一个错误500.由于没有调用该方法,我怀疑Web容器和/或JAX-RS不喜欢我编码的URL位于正确的位置,就像Tomcat似乎一样。

我该如何解决这个问题?另一种方法很好,只要我最终得到一个提供REST服务的独立二进制文件。

1 个答案:

答案 0 :(得分:1)

正如评论中提到的那样,斜线解码存在问题。您可以明确启用解码斜杠:

public static HttpServer startServer() {
    final ResourceConfig rc = new ResourceConfig().packages("dk.kb.dpa.verapdf");
    HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
    httpServer.getHttpHandler().setAllowEncodedSlash(true);
    return httpServer;
}

没有allowEncodedSlash HTTP服务器抛出并在解码编码斜杠期间捕获内部异常。默认情况下禁止斜线解码:

java.io.CharConversionException: Encoded slashes are not allowed
    at org.glassfish.grizzly.http.util.URLDecoder.decode(URLDecoder.java:251)
    at org.glassfish.grizzly.http.util.URLDecoder.decodeAscii(URLDecoder.java:159)
    at org.glassfish.grizzly.http.util.URLDecoder.decode(URLDecoder.java:71)
    at org.glassfish.grizzly.http.util.HttpRequestURIDecoder.decode(HttpRequestURIDecoder.java:161)
    at org.glassfish.grizzly.http.util.RequestURIRef.getDecodedRequestURIBC(RequestURIRef.java:138)
    at org.glassfish.grizzly.http.util.RequestURIRef.getDecodedRequestURIBC(RequestURIRef.java:124)
    at org.glassfish.grizzly.http.server.HttpHandlerChain.doHandle(HttpHandlerChain.java:203)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:242)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:539)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.SameThreadIOStrategy.executeIoEvent(SameThreadIOStrategy.java:103)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.executeIoEvent(AbstractIOStrategy.java:89)
    at org.glassfish.grizzly.nio.SelectorRunner.iterateKeyEvents(SelectorRunner.java:427)
    at org.glassfish.grizzly.nio.SelectorRunner.iterateKeys(SelectorRunner.java:396)
    at org.glassfish.grizzly.nio.SelectorRunner.doSelect(SelectorRunner.java:358)
    at org.glassfish.grizzly.nio.SelectorRunner.run(SelectorRunner.java:281)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573)
    at java.lang.Thread.run(Thread.java:745)
java.io.CharConversionException: Encoded slashes are not allowed
    at org.glassfish.grizzly.http.util.URLDecoder.decode(URLDecoder.java:251)
    at org.glassfish.grizzly.http.util.URLDecoder.decodeAscii(URLDecoder.java:159)
    at org.glassfish.grizzly.http.util.URLDecoder.decode(URLDecoder.java:71)
    at org.glassfish.grizzly.http.util.HttpRequestURIDecoder.decode(HttpRequestURIDecoder.java:161)
    at org.glassfish.grizzly.http.util.RequestURIRef.getDecodedRequestURIBC(RequestURIRef.java:138)
    at org.glassfish.grizzly.http.util.RequestURIRef.getDecodedRequestURIBC(RequestURIRef.java:124)
    at org.glassfish.grizzly.http.server.HttpHandlerChain.doHandle(HttpHandlerChain.java:203)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:242)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:539)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.SameThreadIOStrategy.executeIoEvent(SameThreadIOStrategy.java:103)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.executeIoEvent(AbstractIOStrategy.java:89)
    at org.glassfish.grizzly.nio.SelectorRunner.iterateKeyEvents(SelectorRunner.java:427)
    at org.glassfish.grizzly.nio.SelectorRunner.iterateKeys(SelectorRunner.java:396)
    at org.glassfish.grizzly.nio.SelectorRunner.doSelect(SelectorRunner.java:358)
    at org.glassfish.grizzly.nio.SelectorRunner.run(SelectorRunner.java:281)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573)
    at java.lang.Thread.run(Thread.java:745)