简体   繁体   中英

How to make jersey servlet to load more than one service or class in java REST application

I have Two different java files in the same package. The classes are EntryPoint.java and ModelInn.java . Now, when the jersey servlet starts, I want it to load both the EntryPoint class and ModelInn class. But For the meantime I can only load one. But I want to load the two classes. Am using jetty 9.

Below is the code i used to load EntryPoint java class

package com.rest.test;

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

public class App {
    public static void main(String[] args) throws Exception {
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");

        Server jettyServer = new Server(8080);
        jettyServer.setHandler(context);

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

        // Tells the Jersey Servlet which REST service/class to load.
        jerseyServlet.setInitParameter(
           "jersey.config.server.provider.classnames",
           EntryPoint.class.getCanonicalName());

        try {
            jettyServer.start();
            jettyServer.join();
        } finally {
            jettyServer.destroy();
        }
    }
}

Use either an array or a comma delimited string to pass multiple classes, eg:

setInitParameter("jersey.config.server.provider.classnames", "my.EntryPoint, my.ModelInn");

Reference:

https://jersey.java.net/apidocs/2.23.1/jersey/org/glassfish/jersey/server/ServerProperties.html#PROVIDER_CLASSNAMES

You should probably use package definition instead. If required, you will be able to add multiple packages :

jerseyServlet.setInitParameter("jersey.config.server.provider.packages",
         "com.rest.test.restpackage1;com.rest.test.restpackage2");

Hope it helps !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM