Generating Java Spring-MVC code from Swagger Spec


REST services skeleton code generation is a quick way to get started in implementing the service from popular Swagger spec describing the interface.

In this post we’ll look at generating Java Spring MVC code from Swagger spec. There are a few frameworks out there, but at the time I couldn’t find the one that I want - and so I’ve created a new template for my needs. Here is a quick walkthrough on setting up code-gen for Java Spring MVC project.

Spring MVC Java Templates

There are plenty of projects and templates out there there to generate skeleton server code from Swagger spec, but at the time I haven’t came across one that suit my needs. Ideally it should:

  • Take advantage of Java servlet 3.0+ async feature.

  • Generate code for Spring MVC (my preferred framework)

  • Integrate with the rest of my stack and build tools (Spring Boot + Maven)

  • Generate code directly in target/gen, instead of a new standalone stub that need us to manually copy/paste the bits we want. This will be useful for updating Swagger spec on existing project, otherwise it’s messy trying to manually figure out what is fresh generated code and merge it with existing implementations.

The Solution

Introducing the j8-async template in Swagger-codegen

The default Spring MVC template from Swagger codegen was a good start. The j8-async template is a modified version of it with the following changes:

  • Added <Callable> tag to response to use Spring’s async servlet feature.

  • Generate code with Java 8’s inteface default instead of a plain class.

This new template was merged into swagger-codegen version 2.1.5, you may choose to generate code with it by passing in ‘j8-async’ instead of the ‘default’

<!-- Swagger codegen -->

We’ll also need Spring-Fox dependency for codegen:


And the associated config in swagger-properties for api-docs:


Combining with Maven plugin

Normally the swagger-code gen project just generates stub, this is now what we want. There is a Maven plugin to get around this to generate code into our target/gen - combine this with the Java 8 interface default feature, and we have a generate stub with default implementation nicely integrated with your project.

<!-- Generate REST default interface from Swagger spec -->

Just point it to your swagger spec and pick the ‘j8-async’ option. Because it uses a default interface, it will already work out of the box with the stub response.

Overriding the stub

To provide a custom implementation on the operation, just implemented the gerated API then override the stub method.

For example, here we generate Java stub for the Uber Swagger spec, then override the ‘/me’ operation by overriding the generated ‘MeApi’ interface:

public class MeService implements MeApi {

	// Tip: In Intellj, use alt-insert to select which operation to override.
	public Callable<ResponseEntity<Profile>> meGet() throws NotFoundException {
		return () -> {
			Profile profile = new Profile();
			return new ResponseEntity<>(profile, HttpStatus.OK);

We can pick which operations to override and customise easily as the swagger spec doc changes, no more manually copy/pasting from the stub!

Sample project on Github

The sample project can be found at:

This uses the Uber Swagger spec as mentioned above, and override only the ‘Me’ API with custom inplementation.

Happy Swag Coding!

Post Directory