Search in sources :

Example 1 with JacksonFeature

use of io.dropwizard.jersey.jackson.JacksonFeature in project dropwizard by dropwizard.

the class JerseyClientBuilder method buildConfig.

private Configuration buildConfig(final String name, final ExecutorService threadPool, final ObjectMapper objectMapper, final Validator validator) {
    final ClientConfig config = new ClientConfig();
    for (Object singleton : this.singletons) {
        config.register(singleton);
    }
    for (Class<?> provider : this.providers) {
        config.register(provider);
    }
    config.register(new JacksonFeature(objectMapper));
    config.register(new HibernateValidationBinder(validator));
    for (Map.Entry<String, Object> property : this.properties.entrySet()) {
        config.property(property.getKey(), property.getValue());
    }
    config.register(new DropwizardExecutorProvider(threadPool));
    if (connectorProvider == null) {
        final ConfiguredCloseableHttpClient apacheHttpClient = apacheHttpClientBuilder.buildWithDefaultRequestConfiguration(name);
        config.connectorProvider((client, runtimeConfig) -> createDropwizardApacheConnector(apacheHttpClient));
    } else {
        config.connectorProvider(connectorProvider);
    }
    return config;
}
Also used : JacksonFeature(io.dropwizard.jersey.jackson.JacksonFeature) HibernateValidationBinder(io.dropwizard.jersey.validation.HibernateValidationBinder) ClientConfig(org.glassfish.jersey.client.ClientConfig) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with JacksonFeature

use of io.dropwizard.jersey.jackson.JacksonFeature in project dropwizard by dropwizard.

the class AbstractServerFactory method createAppServlet.

protected Handler createAppServlet(Server server, JerseyEnvironment jersey, ObjectMapper objectMapper, Validator validator, MutableServletContextHandler handler, @Nullable Servlet jerseyContainer, MetricRegistry metricRegistry) {
    configureSessionsAndSecurity(handler, server);
    final String allowedMethodsParam = String.join(",", allowedMethods);
    handler.addFilter(AllowedMethodsFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)).setInitParameter(AllowedMethodsFilter.ALLOWED_METHODS_PARAM, allowedMethodsParam);
    if (enableThreadNameFilter) {
        handler.addFilter(ThreadNameFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    }
    serverPush.addFilter(handler);
    if (jerseyContainer != null) {
        jerseyRootPath.ifPresent(jersey::setUrlPattern);
        jersey.register(new JacksonFeature(objectMapper));
        jersey.register(new HibernateValidationBinder(validator));
        if (registerDefaultExceptionMappers == null || registerDefaultExceptionMappers) {
            jersey.register(new ExceptionMapperBinder(detailedJsonProcessingExceptionMapper));
        }
        handler.addServlet(new ServletHolder("jersey", jerseyContainer), jersey.getUrlPattern());
    }
    final InstrumentedHandler instrumented = new InstrumentedHandler(metricRegistry);
    instrumented.setServer(server);
    instrumented.setHandler(handler);
    return instrumented;
}
Also used : InstrumentedHandler(com.codahale.metrics.jetty9.InstrumentedHandler) JacksonFeature(io.dropwizard.jersey.jackson.JacksonFeature) ExceptionMapperBinder(io.dropwizard.setup.ExceptionMapperBinder) HibernateValidationBinder(io.dropwizard.jersey.validation.HibernateValidationBinder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) AllowedMethodsFilter(io.dropwizard.jersey.filter.AllowedMethodsFilter)

Example 3 with JacksonFeature

use of io.dropwizard.jersey.jackson.JacksonFeature in project dropwizard by dropwizard.

the class JerseyIntegrationTest method configure.

@Override
protected Application configure() {
    final MetricRegistry metricRegistry = new MetricRegistry();
    final SessionFactoryFactory factory = new SessionFactoryFactory();
    final DataSourceFactory dbConfig = new DataSourceFactory();
    dbConfig.setProperties(Collections.singletonMap("hibernate.jdbc.time_zone", "UTC"));
    final HibernateBundle<?> bundle = mock(HibernateBundle.class);
    final Environment environment = mock(Environment.class);
    final LifecycleEnvironment lifecycleEnvironment = mock(LifecycleEnvironment.class);
    when(environment.lifecycle()).thenReturn(lifecycleEnvironment);
    when(environment.metrics()).thenReturn(metricRegistry);
    dbConfig.setUrl("jdbc:h2:mem:DbTest-" + System.nanoTime());
    dbConfig.setUser("sa");
    dbConfig.setDriverClass("org.h2.Driver");
    dbConfig.setValidationQuery("SELECT 1");
    this.sessionFactory = factory.build(bundle, environment, dbConfig, Collections.singletonList(Person.class));
    try (Session session = sessionFactory.openSession()) {
        Transaction transaction = session.beginTransaction();
        session.createNativeQuery("DROP TABLE people IF EXISTS").executeUpdate();
        session.createNativeQuery("CREATE TABLE people (name varchar(100) primary key, email varchar(16), birthday timestamp with time zone)").executeUpdate();
        session.createNativeQuery("INSERT INTO people VALUES ('Coda', 'coda@example.com', '1979-01-02 00:22:00+0:00')").executeUpdate();
        transaction.commit();
    }
    final DropwizardResourceConfig config = DropwizardResourceConfig.forTesting();
    config.register(new UnitOfWorkApplicationListener("hr-db", sessionFactory));
    config.register(new PersonResource(new PersonDAO(sessionFactory)));
    config.register(new PersistenceExceptionMapper());
    config.register(new JacksonFeature(Jackson.newObjectMapper()));
    config.register(new DataExceptionMapper());
    config.register(new EmptyOptionalExceptionMapper());
    return config;
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory) MetricRegistry(com.codahale.metrics.MetricRegistry) EmptyOptionalExceptionMapper(io.dropwizard.jersey.optional.EmptyOptionalExceptionMapper) JacksonFeature(io.dropwizard.jersey.jackson.JacksonFeature) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) Transaction(org.hibernate.Transaction) DropwizardResourceConfig(io.dropwizard.jersey.DropwizardResourceConfig) Environment(io.dropwizard.setup.Environment) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) Session(org.hibernate.Session)

Aggregations

JacksonFeature (io.dropwizard.jersey.jackson.JacksonFeature)3 HibernateValidationBinder (io.dropwizard.jersey.validation.HibernateValidationBinder)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 InstrumentedHandler (com.codahale.metrics.jetty9.InstrumentedHandler)1 DataSourceFactory (io.dropwizard.db.DataSourceFactory)1 DropwizardResourceConfig (io.dropwizard.jersey.DropwizardResourceConfig)1 AllowedMethodsFilter (io.dropwizard.jersey.filter.AllowedMethodsFilter)1 EmptyOptionalExceptionMapper (io.dropwizard.jersey.optional.EmptyOptionalExceptionMapper)1 LifecycleEnvironment (io.dropwizard.lifecycle.setup.LifecycleEnvironment)1 Environment (io.dropwizard.setup.Environment)1 ExceptionMapperBinder (io.dropwizard.setup.ExceptionMapperBinder)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)1 ClientConfig (org.glassfish.jersey.client.ClientConfig)1 Session (org.hibernate.Session)1 Transaction (org.hibernate.Transaction)1