Giter Club home page Giter Club logo

vts-kit-ms-template's Introduction

VTS Kit Spring Boot Microservice Archetype

Archetype for generating a spring-boot service based on Spring Boot 2.6.6

Quickly create your project by following the instructions here

Built-in Feature

Addition feature

Requirement

  • JDK >= 1.8
  • Maven >= 3.6.3
  • IDE: Eclipse or Intellij IDEA

Quick start

  • Run this script to generate new project from this archetype
mvn archetype:generate -DarchetypeGroupId=com.atviettelsolutions -DarchetypeArtifactId=vts-kit-ms-template
  • Enter your project infomation (groupId, artifactId, package,...)

Screenshot

  • Build Generated Project.
mvn clean package
# Skip Unittest
mvn clean package -DskipTests
  • Import Open API Docs URL into API Testing Tool (Ex: Postman)

http://localhost:8080/<artifactId>/api-docs

Screenshot

  • Call Example API

curl --location --request GET 'http://localhost:8080/<artifactId>/book/view/getListBook' --header 'Accept: */*'

Screenshot

Usage

Spring profiles

  • local: Default. Use for Development on local enviroment
  • docker: Use for running on Docker or Kubernetes

Code Structure

This Template Generated Code Structure base on MVC Pattern

Screenshot

Example API

Example API with CRUD operation auto generated for testing

@RestController
@RequestMapping("/book")
public class BookController extends AbstractRestController {
    private BookService bookService;

    @PostMapping("/edit/createBook")
    public ResponseEntity<Object> createBook(@Valid @RequestBody BookDTO bookInfo) {
        return RestUtils.responseOk(bookService.createBook(bookInfo));
    }

    @GetMapping("/view/getBookDetail/{bookId}")
    public ResponseEntity<Object> getBookDetail(@PathVariable("bookId") String bookId) {
        BookDTO rsDelete = bookService.getBookDetail(bookId);
        if(rsDelete == null){
            return RestUtils.response404(InternationalizationUtils.getMessage("msg.error.notFound"));
        }
        return RestUtils.responseOk(rsDelete);
    }

    @PostMapping("/edit/updateBook")
    public ResponseEntity<Object> updateBook(@Valid @RequestBody UpdateBookDTO bookInfo) {
        BookDTO rsUpdate = bookService.updateBook(bookInfo);
        if(rsUpdate == null){
            return RestUtils.response404(InternationalizationUtils.getMessage("msg.error.notFound"));
        }
        return RestUtils.responseOk(rsUpdate);
    }

    @PostMapping("/edit/deleteBook/{bookId}")
    public ResponseEntity<Object> deleteBook(@PathVariable("bookId") String bookId) {
        BookDTO rsDelete = bookService.deleteBook(bookId);
        if(rsDelete == null){
            return RestUtils.response404(InternationalizationUtils.getMessage("msg.error.notFound"));
        }
        return RestUtils.responseOk(rsDelete);
    }

    @GetMapping("/view/getListBook")
    public ResponseEntity<Object> getListBook() {
        return RestUtils.responseOk(bookService.getListBook());
    }

}

API Docs

This Template Built-in supported Open API docs base on Swagger OAS

API Docs Auto Generated at URL: http://localhost:8080/<artifactId>/api-docs

Internationalization

By default, This Template built-in supported Internationalization with 2 language: English and Vietnamese.
Language auto resolve by Request Header Accept-Language

  • First, define message on messages_en.properties and messages_vi.properties file.

messages_en.properties

msg.error.notFound=Infomation not found

messages_vi.properties

msg.error.notFound=Không tìm thấy thông tin
  • Then, Get message in java code by properties key.
String message = InternationalizationUtils.getMessage("msg.error.notFound")

Prometheus metrics

This Template built-in supported spring-boot-starter-actuator for monitoring.

Endpoint: http://localhost:8080/<artifactId>/actuator/prometheus

Mapper

By supported Mapstruct built-in. Mapper binding become easy and quickly

@Mapper(componentModel = "spring")
public interface BookMapper extends IMapper<Book, BookDTO> {

}

Lombok

With Lombok, Getter or Setter Auto generated

@Data
@AllArgsConstructor
@NoArgsConstructor
public class BookDTO {

    private String id;
    private String name;
    private String author;

}

Validation

Use annotation for define validation. Refer Javax Validation Constraints.

Internationalization message built-in supported. Define on validation_en.properties and validation_vi.properties

validation_en.properties

validation.book.nameNotEmpty=Name must not empty
validation.book.authorNotEmpty=Author must not empty

validation_vi.properties

validation.book.nameNotEmpty=Tên không được rỗng
validation.book.authorNotEmpty=Tác giả không được rỗng

Annotation Usage

@Data
@AllArgsConstructor
@NoArgsConstructor
public class BookDTO {

    private String id;

    @NotEmpty(message = "{validation.book.nameNotEmpty}")
    private String name;

    @NotEmpty(message = "{validation.book.authorNotEmpty}")
    private String author;

}

Exception handler

All Exception errors are handled centrally at ExceptionHandler for easy management and maintenance

@ControllerAdvice
public class ExceptionHandler extends AbstractExceptionHandler {

    @Override
    protected ResponseEntity<ErrorDTO> handleAccessDeniedException() {
        return super.handleAccessDeniedException();
    }

    @Override
    protected ResponseEntity<ErrorDTO> handleCommonException(Exception ex) {
        return super.handleCommonException(ex);
    }

    @Override
    protected ResponseEntity<ErrorDTO> handleMethodArgumentException(MethodArgumentNotValidException ex) {
        return super.handleMethodArgumentException(ex);
    }

    @Override
    protected ResponseEntity<ErrorDTO> handleConstraintViolationException(ConstraintViolationException ex) {
        return super.handleConstraintViolationException(ex);
    }

    @Override
    protected ResponseEntity<ErrorDTO> handleMissingPartException(Exception ex) {
        return super.handleMissingPartException(ex);
    }
}

Contribute

Setting up the development environment

  • IDE: Eclipse, Intellij IDEA
  • JDK: >= JDK 8
  • Maven: >= 3.6.0
  • Build:
# cd archetype directory
cd archetype
# build
mvn clean install -DskipTests

Contribute Guidelines

If you have any ideas, just open an issue and tell us what you think.

If you'd like to contribute, please refer Contributors Guide

License

This code is under the MIT License.

See the LICENSE file for required notices and attributions.

vts-kit-ms-template's People

Contributors

viettel-solutions avatar hkphu-it avatar zerodn avatar

Stargazers

Luyện Hải Đăng avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.