JAX-RS with embedded server

前端 未结 3 2049
自闭症患者
自闭症患者 2020-12-08 05:16

Clarification: this question was about GZIPping an JAX-WS-based REST service, but I\'ve decided to change the topic to make it easier to find

相关标签:
3条回答
  • 2020-12-08 05:51

    gzipping the output is the reponsibility of JAX WS implementation. You should refer to server's (Tomcat, Glassfish, JBoss, etc) documentation in order to configure your http network listeners.

    0 讨论(0)
  • 2020-12-08 06:01

    If using CXF for your JAX-WS implementation (or JAX-RS), you could just add @GZIP annotation onto the service class.

    0 讨论(0)
  • 2020-12-08 06:13

    If you really want to do REST with Java I would suggest you to to use a JAX-RS implementation (RESTeasy, Jersey...).

    If your main concern is the dependency on a servlet container, you could use the JAX-RS RuntimeDelegate to register your application as a JAX-RS endpoint.

    // Using grizzly as the underlaying server
    SelectorThread st = RuntimeDelegate.createEndpoint(new MyApplication(), SelectorThread.class);
    
    st.startEndpoint();
    
    // Wait...
    st.stopEndpoint();
    

    Concerning GZIP encoding, each JAX-RS provider has different approaches. Jersey provides a filter to accomplish the encoding transparently. RESTEasy provides an annotation for that.

    EDIT

    I did some small tests. The following two things will definitely work for you, assuming you are using Maven.

    Using Jersey + SimpleServer:

        public static void main( String[] args ) throws Exception {
    
        java.io.Closeable server = null;
    
        try {
            // Creates a server and listens on the address below.
            // Scans classpath for JAX-RS resources
            server = SimpleServerFactory.create("http://localhost:5555");
            System.out.println("Press any key to stop the service...");
            System.in.read();
        } finally {
            try {
                if (server != null) {
                    server.close();
                }
            } finally {
                ;
            }
        }
    }
    

    with maven dependencies

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.10</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-simple-server</artifactId>
        <version>1.10</version>
    </dependency>
    

    Or using the Jersey + Grizzly2:

    public static void main(String[] args) throws Exception {
    
        HttpServer server = null;
    
        try {
            server = GrizzlyServerFactory.createHttpServer("http://localhost:5555");
            System.out.println("Press any key to stop the service...");
            System.in.read();
        } finally {
            try {
                if (server != null) {
                    server.stop();
                }
            } finally {
                ;
            }
        }
    }
    

    with maven dependencies

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.10</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-grizzly2</artifactId>
        <version>1.10</version>
    </dependency>
    

    Honestly speaking I was not able to get the RuntimeDelegate sample working, too. There certainly is a way to start RESTEasy out of the box, too but I cannot recall it at the moment.

    0 讨论(0)
提交回复
热议问题