Building REST API with Swagger

Building REST API with Swagger

Swagger is one of the most popular specifications for developing APIs on the market right now.

There are different ways how to work with it. I’ll describe my practical experience.

Code generation

By this approach, the REST API is described in a human- (YAML) or machine-readable (JSON) way.
The client/server code is generated with Swagger codegen.

Pros:

  • You can write YAML API specification befor decision about the implementation language
Contras:
  • additional step to generate source stub + manual copy generated java interfaces to target project
  • not possible to rename the class for REST DTO, because it is used for model definition and will cause a lot compile errors in generated clients

YAML generation. Official swagger way

There are several frameworks to generate YAML for Swagger. In Java many of these frameworks rely heavily on annotations. The official swagger library works that way.

Pros:

  • todo
Contras:
    • more annotations as actual code

  • documentation / annotation / Javadoc reflects sometimes not to 100% your Java backend code

If you happen to do contract first, it is not a pleasant experience to write the YAML yourself for non-trivial APIs even with the swagger editor.

YAML generation with JAX-RS Analyzer

This is my preferred way to design REST API.
If you use Java JAX-RS for REST backend, the JAX-RS Analyzer would be my recomendation.
It generates an overview of all JAX-RS resources in a project by bytecode analysis (not just by reflection).

Pros:

  • Genuine docs. Generated Swagger yaml/json specification reflects your Java backend code and always actual
  • Readability. You don’t need additional swagger annotations on your JAX-RS resource methods
Contras:
  • todo

Apache camel shared datasource

This short comment intends to explain the usage of HSQLDB as a shared data source for Apache camel using camel-blueprint. As a result, any possible camel routes can be linked to the created shared data source. Furthermore, you can use a JDBC database of your choice.

You need to install the following bundles:

  • camel-blueprint
  • camel-stream
  • camel-csv
  • camel-sql

HikariCP is used as JDBC connection pool.

The camel-blueprint route for CSV file with pipe “|” delimiter:

Java 8 – Reading a file as String

There are a lot of examples how to read a file content in Java 8.
The most common one is

 However, you can receive different results for the same file on different operation systems! 

Use always a charset with String to Byte array transformations and vice versa to achieve a cross-platform identical output from this code section:

Travelling salesman algorithm in Java Fork/Join Framework

travelling salesman problemTravelling salesmann problem”  is a NP-equivalent problem. The implementation using the Java Fork / Join framework contains the Nearest Neighbour Algorithm. The goal is to provide a solution as soon as possible. However, as time passes, the provided solution of the algorithm becomes even better. The information points ( a. cities ) are encapsulated in TSPPoint objects. The first point is the start.
In the beginning the distance matrix is calculated:

CostX and CostY – are the axle weight.

E.g .: maximum axis velocity of the robot.

An edge is calculated by the TSPAction class. The recursive function returns no results , so TSPAction inherits the RecursiveAction class. You just have to implement the function TSPAction.compute ( ) , which is responsible for the whole logic and heuristics.

Heuristics:

  • the getNextNearestPoint function selects the minimal weighted, adjacent edge to the next available node (local free points list ) .
  • in lines 19-23 the path with the minimal weight is searched first . All longer pathes are thrown away ( lines 4-7 )

 Source code download

This is how it looks like in real life:

Directory stream with Java 8 lambda

Short snippet how to leave 100 newest text files in directory and delete others with help of Java 8 lambda.