- Java SDK 1.8+
- Maven or gradle build tool
To efficently manage Java and build tools verions you can use SDKMAN!.
Scope: deliver an read API for exposing data from DocumentDB.
In order to provide a consistent, usable and maintaneble microservice we need to provide, for our MVP, at least:
- Feature #capitan-obvious
- Model
- Controller
- Service
- Dockerfile spec
- Logging config
- ...
- Documentantion (ideally README.md in the root folder) with:
Starting a Spring boot project is pretty easy, you have to options:
- Using Sprint Initializr
- Using spring-boot-cli available on SDKMAN!!
Make sure to select at least one dependencies for implementing Rest repositories.
Our domain model: what we need to work with. For the sake of semplicity our model will be straightforward and will not need any DTO.
Is the meta interface for accessing out Mongo Database.
Create a ModelRepository.java with:
@Repository
public interface ModelRepository extends MongoRepository<Model, String> {
List<Model> findByField(String field);
}Repositories are convention driven see here for a detailed explanation.
In a layered architecture the controller is responsible to transmit/receive data from the transport channel (ex. HTTP/S or WebSockets). The service contains all the plumbings and the business logic needed.
To make the things easier, in this lab you will use our Repository directly in the Controller.
Be aware: this is not a best practice.
Create ModelController.java:
@RestController
@RequestMapping("/api/endpoint")
public class ModelController {
private ModelRepository modelRepository;
@Autowired
public ModelController(ModelRepository modelRepository) {
this.modelRepository = modelRepository;
}
@GetMapping
public List<Workshop> findAll() {
return Lists.newArrayList(modelRepository.findAll());
}
}Exercises:
- try implementing a GET endpoint to search by authors.
- try implementing a POST endpoint to save one
Model.java.
Swagger is a common lib for documenting our endpoints.
Add Springfox dependency to your pom.xml:
<!-- Define a property ala <java.version /> -->
<springfox.version>2.9.2</springfox.version>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox.version}</version>
</dependency>Create SwaggerConfig.java in the root dir:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return
new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}You can now view the generated manifest on /v2/api-docs path and access the UI on /swagger-ui.html path.
Exercise:
- try exposing only the controllers you implemented (ex. those with
@RestControllerannotation).
Spring Boot Actuator module provides production-ready features like health checks, metrics, auditing etc. All of these features can be consumed over HTTP endpoints or JMX.
Update your pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>Modify application.yml:
info:
app:
name: Workshop 04
description: Microservice with Docker container
java:
version: ${java.version}
management:
endpoint:
health:
show-details: alwaysFROM image
WORKDIR /app
COPY . /app
RUN command
EXPOSE port
CMD do something on run
Exercise:
- Fill the gaps.