Feature/bael 338 introduction dropwizard (#8572)
* [ BAEL-3388 ] : Introduction Dropwizard * BAEL-3388: Fix formatting
This commit is contained in:
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.dropwizard.introduction;
|
||||
|
||||
import com.baeldung.dropwizard.introduction.configuration.ApplicationHealthCheck;
|
||||
import com.baeldung.dropwizard.introduction.configuration.BasicConfiguration;
|
||||
import com.baeldung.dropwizard.introduction.domain.Brand;
|
||||
import com.baeldung.dropwizard.introduction.repository.BrandRepository;
|
||||
import com.baeldung.dropwizard.introduction.resource.BrandResource;
|
||||
import io.dropwizard.Application;
|
||||
import io.dropwizard.configuration.ResourceConfigurationSourceProvider;
|
||||
import io.dropwizard.setup.Bootstrap;
|
||||
import io.dropwizard.setup.Environment;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class IntroductionApplication extends Application<BasicConfiguration> {
|
||||
|
||||
public static void main(final String[] args) throws Exception {
|
||||
new IntroductionApplication().run("server", "introduction-config.yml");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(final BasicConfiguration basicConfiguration, final Environment environment) {
|
||||
final int defaultSize = basicConfiguration.getDefaultSize();
|
||||
final BrandRepository brandRepository = new BrandRepository(initBrands());
|
||||
final BrandResource brandResource = new BrandResource(defaultSize, brandRepository);
|
||||
environment
|
||||
.jersey()
|
||||
.register(brandResource);
|
||||
|
||||
final ApplicationHealthCheck healthCheck = new ApplicationHealthCheck();
|
||||
environment
|
||||
.healthChecks()
|
||||
.register("application", healthCheck);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(final Bootstrap<BasicConfiguration> bootstrap) {
|
||||
bootstrap.setConfigurationSourceProvider(new ResourceConfigurationSourceProvider());
|
||||
super.initialize(bootstrap);
|
||||
}
|
||||
|
||||
private List<Brand> initBrands() {
|
||||
final List<Brand> brands = new ArrayList<>();
|
||||
brands.add(new Brand(1L, "Brand1"));
|
||||
brands.add(new Brand(2L, "Brand2"));
|
||||
brands.add(new Brand(3L, "Brand3"));
|
||||
|
||||
return brands;
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.dropwizard.introduction.configuration;
|
||||
|
||||
import com.codahale.metrics.health.HealthCheck;
|
||||
|
||||
public class ApplicationHealthCheck extends HealthCheck {
|
||||
@Override
|
||||
protected Result check() throws Exception {
|
||||
return Result.healthy();
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.dropwizard.introduction.configuration;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.dropwizard.Configuration;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
public class BasicConfiguration extends Configuration {
|
||||
@NotNull private final int defaultSize;
|
||||
|
||||
@JsonCreator
|
||||
public BasicConfiguration(@JsonProperty("defaultSize") final int defaultSize) {
|
||||
this.defaultSize = defaultSize;
|
||||
}
|
||||
|
||||
public int getDefaultSize() {
|
||||
return defaultSize;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.dropwizard.introduction.domain;
|
||||
|
||||
public class Brand {
|
||||
private final Long id;
|
||||
private final String name;
|
||||
|
||||
public Brand(final Long id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.dropwizard.introduction.repository;
|
||||
|
||||
import com.baeldung.dropwizard.introduction.domain.Brand;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class BrandRepository {
|
||||
private final List<Brand> brands;
|
||||
|
||||
public BrandRepository(final List<Brand> brands) {
|
||||
this.brands = ImmutableList.copyOf(brands);
|
||||
}
|
||||
|
||||
public List<Brand> findAll(final int size) {
|
||||
return brands
|
||||
.stream()
|
||||
.limit(size)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public Optional<Brand> findById(final Long id) {
|
||||
return brands
|
||||
.stream()
|
||||
.filter(brand -> brand
|
||||
.getId()
|
||||
.equals(id))
|
||||
.findFirst();
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.dropwizard.introduction.resource;
|
||||
|
||||
import com.baeldung.dropwizard.introduction.domain.Brand;
|
||||
import com.baeldung.dropwizard.introduction.repository.BrandRepository;
|
||||
|
||||
import javax.ws.rs.*;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Path("/brands")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public class BrandResource {
|
||||
private final int defaultSize;
|
||||
private final BrandRepository brandRepository;
|
||||
|
||||
public BrandResource(final int defaultSize, final BrandRepository brandRepository) {
|
||||
this.defaultSize = defaultSize;
|
||||
this.brandRepository = brandRepository;
|
||||
|
||||
}
|
||||
|
||||
@GET
|
||||
public List<Brand> getBrands(@QueryParam("size") final Optional<Integer> size) {
|
||||
return brandRepository.findAll(size.orElse(defaultSize));
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/{id}")
|
||||
public Brand getById(@PathParam("id") final Long id) {
|
||||
return brandRepository
|
||||
.findById(id)
|
||||
.orElseThrow(RuntimeException::new);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
defaultSize: 5
|
||||
Reference in New Issue
Block a user