Moises Gamio
Moises Gamio Software Engineer. Comprehensive experience in all phases of the software development lifecycle for several economic sectors.

Building a RESTful Web Service with Gradle and Openapi

Building a RESTful Web Service with Gradle and Openapi

This guide walks you through the process of creating a RESTful web service with Gradle, Spring and Openapi.

What You Will Building

You will build a service that will implement a Finance API specified in API Hub Powered by Swagger.

Starting with Spring Initializr

To manually initialize the project:

  1. Navigate to https://start.spring.io and setup the following configuration:

springInitializrGradle

  1. Click GENERATE.

  2. Download the resulting ZIP file, which is an archive of a web application that is configured with the choices above.

  3. Open the project in your favourite IDE.

Gradle

Gradle automates the building, testing, and deployment of software from information in build scripts.

codersite

To import all dependencies in your local machine, execute in your IDE Terminal the following Gradle command:

1
C:\..\apifinance> .\gradlew build

We create the following packages and classes:

gradleProject

For the integration between spring-boot and swagger-ui, add the following library to the list of your project dependencies.

1
2
3
4
5
6
dependencies {
  .
  .
  .
  implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.11'
}

And execute .\gradlew clean build to download all dependencies again.

We create an Interface to declare the funcionalities of this API by using swagger annotations.

1
2
3
4
5
6
7
8
9
10
@Tags(value = {@Tag(name = "timeValueOfMoney")})
@RequestMapping("/v1/timeValueOfMoney")
public interface TimeValueOfMoneyApi {

  @RequestMapping(value = "/simpleInterest",
    produces = {"application/json"},
    method = RequestMethod.GET)
  ResponseEntity<SimpleInterestResponse> getSimpleInterest() throws Exception;
  
}

We refactor the getSimpleInterest method to include all request parameters with its documentation by using swagger annotations.

Here is the code that corresponds to the principal parameter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  @Operation(
    summary = "Gets the calculated simple interest",
    description = "Gets the calculated simple interest for the requested parameters",
    tags={ "timeValueOfMoney" })
  @ApiResponses(value = {
    @ApiResponse(responseCode = "200", description = "OK", content = @Content(mediaType = "application/json", 
      schema = @Schema(implementation = SimpleInterestResponse.class)))
  })
  @RequestMapping(value = "/simpleInterest",
    produces = {"application/json"},
    method = RequestMethod.GET)
  ResponseEntity<SimpleInterestResponse> getSimpleInterest(
    @Parameter(in = ParameterIn.QUERY, description = "is the principal amount", schema = @Schema())
    @Valid @RequestParam(value = "principal", required = true)
    Integer principal
  ) throws Exception;
codersite

Run the ApiFinanceApplication. By default an Apache Tomcat is initialized as a Servlet engine. See the api doc at http://localhost:8080/swagger-ui/index.html.

apiFinanceInLocalhost

We create a @RestController to manage the requested parameters. And we delegate the calculus of our formula to a specialized TimeValueOfMoneyService Class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@RestController
public class TimeValueOfMoneyApiController implements TimeValueOfMoneyApi {

  private final TimeValueOfMoneyService timeValueOfMoneyService;

  public TimeValueOfMoneyApiController(
    TimeValueOfMoneyService timeValueOfMoneyService) {
    this.timeValueOfMoneyService = timeValueOfMoneyService;
  }

  @Override
  public ResponseEntity<SimpleInterestResponse> getSimpleInterest(
    Integer principal,
    Integer interestRate,
    String unitOfTime,
    Integer time,
    String yearDaysConvention) throws Exception {

    SimpleInterestResponse response  = timeValueOfMoneyService.getSimpleInterest(
      principal,
      interestRate,
      unitOfTime,
      time,
      yearDaysConvention);

    return new ResponseEntity<SimpleInterestResponse> (response, HttpStatus.OK);
  }
}

We create a class that implements the previous Service.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class TimeValueOfMoneyServiceImpl implements TimeValueOfMoneyService {
  @Override
  public SimpleInterestResponse getSimpleInterest(
    Integer principal,
    Integer interestRate,
    String unitOfTime,
    Integer time,
    String yearDaysConvention) throws Exception {

    SimpleInterestResponse response = new SimpleInterestResponse();

    // Implement here ...

    return response;
  }
}

Well, we arrive to the magic of programming. Here is where we use all our creativity to implement a useful service with the help of Unit Testing. Follow me in my next article!

You can also fork the project from Github and open it in your IDE or other editor.

Please support me as a writer. Every contribution helps, and your donation can help add more articles to this website, no matter how small. Thank you!

You can also buy my little book about Data structures and Algorithms. As a Java Developer, one is expected to attend interviews every now and then and I am pretty sure this book will be a handy guide.

The difference between passing or failing your next technical interview is how you handle data structures. This book takes you straight to the point. Invest in your future: master algorithms today, land the job tomorrow!
codersite