Java – how do I update the sample to use the latest versions of Jetty (9.1.0. RC2) and Jersey (2.7)?

I've been trying to follow this example, but I'm not lucky

At the suggestion of the following reviewers, I decided to update the example to use the latest versions of Jetty (9.1.0. RC2) and Jersey (2.7)

This is an updated POM with updated dependencies:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>HelloJerseyLatest</groupId>
<artifactId>HelloJerseyLatest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.7</version>
    </dependency>

    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>9.1.0.RC2</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>9.1.0.RC2</version>
    </dependency>
</dependencies>
</project>

The first thing to note is that the Jersey package has been downloaded from com sun. jersey. spi.* Change to org.org glassfish. jersey.*. Therefore, the main methods also need to be changed:

package example.server;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

//import com.sun.jersey.spi.container.servlet.ServletContainer;
import org.glassfish.jersey.servlet.ServletContainer;

public class MinimalServerRest {

     public static void main(String[] args) throws Exception {
         ServletHolder sh = new ServletHolder(ServletContainer.class);

         // these initialization strings will need to be updated.
         sh.setInitParameter("com.sun.jersey.config.property.resourceConfigClass","com.sun.jersey.api.core.PackagesResourceConfig");
         sh.setInitParameter("com.sun.jersey.config.property.packages","rest");//Set the package where the services reside
         sh.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature","true");

         Server server = new Server(9999);
         ServletContextHandler context = new ServletContextHandler(server,"/",ServletContextHandler.SESSIONS);
         context.addServlet(sh,"/*");
         server.start();
         server.join();
      }
}

What changes are required to update the original sample code to the latest version? I don't have a web XML file Do I need one?

Solution

I realize that this is not an example of your work (your example link is broken) – I don't know much about Jersey 1 and it's difficult to try to upgrade other people's projects When you have another question asking for a HelloWorld example, I think you just need something to see JERSEY & pier for yourself

So here you go – two examples, one using jettyhttpcontainerfactory and the other using Jersey servletcontainer

The first is Jersey resources - very simple This will use the "test" path to set the class and the Hello path to set a method that accepts get. Net that generates "Hello world" in plain text

@Path("/test")
public class TestResource {

    @GET
    @Path("hello")
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
       return "Hello World";
    }
}

Next is the server class:

public class ExampleServer {

    public static void main(String[] args) {

            URI baseUri = UriBuilder.fromUri("http://localhost/").port(9998).build();
            ResourceConfig config = new ResourceConfig(TestResource.class);
            Server server = JettyHttpContainerFactory.createServer(baseUri,config);
       }
}

Finally, there are POM dependencies (note that both examples have dependencies)

<dependencies>
            <dependency>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-server</artifactId>
                <version>9.1.3.v20140225</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-servlet</artifactId>
                <version>9.1.3.v20140225</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.core</groupId>
                <artifactId>jersey-server</artifactId>
                <version>2.7</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.containers</groupId>
                <artifactId>jersey-container-servlet-core</artifactId>
                <version>2.7</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.containers</groupId>
                <artifactId>jersey-container-jetty-http</artifactId>
                <version>2.7</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.media</groupId> 
                <artifactId>jersey-media-moxy</artifactId> 
                <version>2.7</version> 
            </dependency>
            <!-- if you want to enable JSON support,include Moxy and Jersey will automatically enable the Feature -->

      </dependencies>

See also https://jersey.java.net/apidocs/2.7/jersey/javax/ws/rs/core/Feature.html For functionality – by including Moxy in the classpath, Jersey will automatically register the moxyjsonfeature If you prefer to use Jackson, you need to manually register the Jackson feature and dependencies You can register any function (separated by commas) in the same initialization parameter of the registered resource

If you want to configure as a servlet, use it as the exampleserver code

public class ExampleServer {

    public static void main(String[] args) throws Exception {

            Server server = new Server(9998);

            ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
            context.setContextPath("/");

            server.setHandler(context);

            ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class,"/*");
            jerseyServlet.setInitOrder(0);

            /*This parameter tells the Jersey Servlet which of your REST resources to load. In this example we're adding the TestResource class. Jersey will then invoke this class for requests coming into paths denoted by the @Path parameter within the TestResource class. If you have multiple classes,you can either list them all comma separated,of use "jersey.config.server.provider.packages" and list the package name instead */
            jerseyServlet.setInitParameter("jersey.config.server.provider.classnames","foo.bar.TestResource");
            server.start();
            server.join();
       }
}

Notice the servlet version, I'm defining the class name of the resource If you have some, you'd better use Jersey config. server. provider. Packages provides the package name

I hope this will help If you have any questions, please let me know

take

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>