Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] How do I tell embedded jetty to scan for annotations?

Hello,

I am trying to configure embedded jetty so that it scans for the @WebListener annotation. I have added the Jetty-annotations library to my pom.xml file. From looking at this documentation, it looks like I also need to set metadata-complete to false. The problem is that most of the documentation references a web.xml or jetty.xml which I don't have. Is it possible to configure Jetty to scan for annotations without it? Is there a way I can configure the server to tell it where the @WebListener is?

Here is the method where I am creating and configuring the server:

public static void start() throws Exception {
// Create Embedded Jetty server
jettyServer = new Server();

// Configure for Http
HttpConfiguration http_config = new HttpConfiguration();
http_config.setSecureScheme("https");
http_config.setSecurePort(8443);
http_config.setOutputBufferSize(32768);

ServerConnector http = new ServerConnector(jettyServer,
new HttpConnectionFactory(http_config));
http.setPort(HTTP_SERVER_PORT);
http.setIdleTimeout(30000);
jettyServer.addConnector(http);

// Add ServletContextHandler
ServletContextHandler servletContextHandler = new ServletContextHandler(
ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");

jettyServer.setHandler(servletContextHandler);

// Add API Origin Filter
servletContextHandler.addFilter(
"com.my.company.swagger.api.util.ApiOriginFilter", "/*",
EnumSet.of(DispatcherType.INCLUDE, DispatcherType.REQUEST));

// Setup API resources (Jersey)
ServletHolder jerseyServlet = new ServletHolder(new ServletContainer());
jerseyServlet.setInitOrder(1);
jerseyServlet.setInitParameter(
"com.sun.jersey.spi.container.ContainerRequestFilters",
"com.sun.jersey.api.container.filter.PostReplaceFilter");
jerseyServlet.setInitParameter(
"com.sun.jersey.api.json.POJOMappingFeature", "true");
jerseyServlet
.setInitParameter(
ServerProperties.PROVIDER_PACKAGES,
"io.swagger.jaxrs.json;io.swagger.jaxrs;io.swagger.jaxrs.listing;com.my.company.swagger.api");
jerseyServlet.setInitParameter(ServerProperties.WADL_FEATURE_DISABLE,
"true");
jerseyServlet
.setInitParameter(
ServerProperties.PROVIDER_CLASSNAMES,
"org.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.media.multipart.MultiPartFeature");
servletContextHandler.addServlet(jerseyServlet, "/api/*");

jettyServer.setHandler(servletContextHandler);

// Start the server
jettyServer.start();
}

And this is my class with the @WebListener

package com.my.company.swagger.api.util;

import io.swagger.jaxrs.config.BeanConfig;

import javax.annotation.Resource;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@WebListener
public class SwaggerInitializer implements ServletContextListener {

private static final Logger LOGGER = LoggerFactory.getLogger(SwaggerInitializer.class);

    public void contextInitialized(ServletContextEvent servletContextEvent) {
    LOGGER.debug("######### Configuring Swagger with BeanConfig #########");
        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setVersion( "1.0.0" );
        beanConfig.setResourcePackage( "com.my.company.swagger.api" );
        beanConfig.setBasePath( "http://localhost:12043/api-docs" );
        beanConfig.setDescription( "Hello World!" );
        beanConfig.setTitle( "Swagger Test Server" );
        beanConfig.setScan( true );
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    }
   
}

Thank you!

    Jennifer

Back to the top