How to start writing a microservice in Spring Boot so that you don't have a headache later

Hello! My name is Zhenya, I am a Java developer at Usetech, recently I have been working a lot with microservice architecture, and in this article I would like to share some points that may be useful to pay attention to when you write a new microservice on Spring Boot.



Experienced developers may find these recommendations obvious, but they are all taken from the practice of working on real projects.



1. Leaving the controllers thin



In a traditional layered architecture, the controller class accepts requests and routes them to the service, while the service handles the business logic. However, sometimes in the methods of the controller you can find some kind of validation of the input parameters, as well as the transformation of the Entity into DTO.



For instance:



@GetMapping
public OperationDto getOperationById(@PathVariable("id") Long id) {

    Optional<Operation> operation = operationService.getById(id);

    if (operation.isEmpty()) {
        return EMPTY_OPERATION_DTO;
    }

    OperationDto result = mapperFacade.map(operation.get(), OperationDto.class);
    return result;
}


On the one hand, the mapping takes only one line, and the check for the absence of a result looks quite logical. However, in such a case, the principle of the sole responsibility of the controller is violated. While validation or mapping is simple, a couple of extra lines of code in the controller method are not at all striking, but in the future, the logic of both validation and mapping may become more complicated, and then it will become obvious that the controller not only accepts and redirects requests, but also deals with business logic.



, , , "", , DTO.



:



@GetMapping
public OperationDto getOperationById(@PathVariable("id") Long id) {
    return operationService.getById(id);
}


:



public OperationDto getById(Long id) {

    Optional<Operation> operationOptional = ... //  operation

    return operationOptional
        .map(operation -> mapperFacade.map(operation, OperationDto.class))
        .orElse(EMPTY_OPERATION_DTO);
}


2. DTO



, DTO REST API, DTO Kafka. , REST Kafka, , DTO . , DTO .



, DTO, , DTO, , . DTO , .



3. WARN-,



, , , , , Spring Boot , , .



WARN, "" Spring Boot 2 Hibernate:

spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning



, - Spring Boot 2 Open Session In View, Hibernate HTTP-.



Open Session In View LazyInitializationException, , Hibernate , , -. , , , ( n+1). .



, Open Session In View , — application.yml :



spring:
  jpa:
    open-in-view: false


4.



, @SpringBootTest , , . , @SpringBootTest, , Spring . , . , , .



:



  • @Import,
  • @ActiveProfiles
  • @MockBean Mockito — , -
  • @TestPropertySource — ,
  • @DirtiesContext — ,


, .. . , :



  • @SpringBootTest
  • @ActiveProfiles("test")
  • protected ( @Autowired) - (@MockBean)


, (@AfterEach), / .



, , , , setUp .



@DirtiesContext.



, - .




All Articles