Tutorial 3 : Spring Boot Rest web services using Swagger and Orika

Tutorial 3 : Spring Boot Rest web services using Swagger and Orika

Getting Started with Swagger

When creating a REST API, good documentation is instrumental.
Moreover, every change in the API should be simultaneously described in the reference documentation.

What is swagger

  • Swagger is a specification for documenting REST API. As an application developer and write web services using your favorite framework.
  • Swagger scans the code and exposes the documentation on some URL.
  • Any client can consume this URL (which comes as XML or JSON documents) and learn how to use your REST web services.
  • Client now knows which HTTP methods to call on which URL, which input documents to send, which status code to expect, etc.
  • Swagger is a specification and supports a wide range of frameworks.

Installation

In order to use Swagger to a web Spring application you need to add the dependencies to the Springfox and Swagger libraries in the pom.xml file

configure swagger 2 in the Project

To enable support for swagger specification 2.0 use the annotation
To document the service we use a Docket. This is changed to be more inline with the fact that expressing the contents of the documentation is agnostic of the format the documentation is rendered.
src/main/java/com/intellitech/springlabs/SwaggerConfig.java

In all controller we need to use Swagger annotations to document the API.
src/main/java/com/intellitech/springlabs/controller/UserController.java

The CustomError is a class used to return error message in the response
src/main/java/com/intellitech /springlabs/util/CustomError.java

Getting Started With Orika

 

Development of enterprise software often require creation of APIs for each application components. On implementation phase this involves mapping between API models on different layers of components (e.g. mapping persistence model to domain model to DTO). Such mapping often prone to boiler plate code, consuming development efforts and time.

The good solution is that serveral alternatives exist for object-to-object mapping with Orika (https://orika-mapper.github.io). Although this project is quite new (since 2012), it has a very stable code base, offers all relevant features and is very easy to understand.

Installation

Configure Orika with the spring framework

Define the source and target beans.

Source bean

src/main/java/com/intellitech/springlabs/model/User.java

src/main/java/com/intellitech/springlabs/model/Device.java

src/main/java/com/intellitech/springlabs/model/Role.java

Target Bean

src/main/java/com/intellitech/springlabs/model/dto/UserDto.java

src/main/java/com/intellitech/springlabs/model/dto/DeviceDto.java

src/main/java/com/intellitech/springlabs/model/dto/RoleDto.java

The basic cornerstone of the Orika mapping framework is the MapperFactory class. This is the class which you will use to configure any mappings, and obtain the MapperFacade which performs the actual mapping work.

src/main/java/com/intellitech/springlabs/model.mapper/UserMapper.java

src/main/java/com/intellitech/springlabs/model.mapper/RoleMapper.java

src/main/java/com/intellitech/springlabs/model.mapper/DeviceMapper.java

src/main/java/com/intellitech/springlabs/repository/DeviceRepository.java

Service Layer

Now let´s create UserService,DeviceService and RoleService layer(interface and implementation). We will inject the UserRepository,DeviceRepository and RoleRepository into our services implementation.
src/main/java/com/intellitech/springlabs/service/UserService.java

src/main/java/com/intellitech/springlabs/service/DeviceService.java

src/main/java/com/intellitech/springlabs/service/RoleService.java

src/main/java/com/intellitech/springlabs/service/impl/UserServiceImpl.java

src/main/java/com/intellitech/springlabs/util/MapperUtil.java

src/main/java/com/intellitech/springlabs/controller/UserController.java

src/main/java/com/intellitech/springlabs/controller/LoginController.java

Run the application

First you need to create a database with name and run the script in PostgreSQL database :

Now, run the application by launching the SpringBootApplication class using the command mvn spring-boot:run and visit and authenticate with one of the two users:
: username=user, password=user
: username=admin, password=admin

Source code

https://bitbucket.org/intellitech-team/spring-labs/src/da6f67a63d9be72c51a38e516350815ac456e404/Lab3/?at=master

Author: Nizar Ellouze

3 Comments
  • Posted at 10:39 pm, May 21, 2017

    Thanks for a nice tutorial. How about providing an example using Stored Procedures.

    BTW, you spelled “several” incorrectly more than once.

    cheers.

  • Daniel
    Reply
    Posted at 3:45 pm, July 22, 2017

    great!. What about mapstruct?.

  • Muthuraman Ponraja
    Reply
    Posted at 9:19 pm, July 26, 2017

    Nice Dinesh, really helpful,. a jump starter

Post a Comment

Comment
Name
Email
Website