Building Microservices with Spring Boot and Apache Thrift. Part 2. Swifty services

In previous article I showed you how to use pure Apache Thrift to create protocols for your services. This time I will explain how to use Facebook Swift (not the best name for project BTW :)) to get rid of Thrift IDL files for server code.

Few words about Swift. Swift - Java annotation-based generator for Apache Thrift. It's priceless when you develop your APIs in Java and want to expose them to the other world using Apache Thrift.


Lets recreate our previous example ( https://github.com/bsideup/spring-boot-thrift ) with it. Start with simple build.gradle file:

Nothing special, Spring Boot with few Facebook Swift libraries.

Next we need to add Swift protocol classes:

Exception is simple Swift struct which extend Exception (See Ma no TException)

Enums are handled with Swift, so we don't need to annotate them (but we can)

Next - application main class for Spring Boot:


Now we're ready to write some tests:

As you can see, only difference here (compared to Thrift version) is setUp method.
Diff with Thrift version


We still have no Swift service implementation. Implementation of handler looks almost the same as previous:

Diff with Thrift version

Now if you will run tests you should see all tests green.

Thrift integration

But hey, how about other non-Java consumers of service? Don't worry, Swift comes with a tool for generating *.thrift files from annotated Java classes: https://github.com/facebook/swift/tree/master/swift2thrift-generator-cli

Example output for our service will be:


Full source files for this example can be found at GitHub: https://github.com/bsideup/spring-boot-swift

Next time I will show you how to write Async Thrift services using Swift with minimal changes. Stay tuned!