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

人盡茶涼 提交于 2021-02-08 11:44:25

问题


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();
        }
    }
}

回答1:


Use either an array or a comma delimited string to pass multiple classes, e.g.:

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




回答2:


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 !



来源:https://stackoverflow.com/questions/38206312/how-to-make-jersey-servlet-to-load-more-than-one-service-or-class-in-java-rest-a

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!