Pavel Petrashov
Pavel Petrashov

Reputation: 1262

How can I create API documentation for API built via Sparkjava?

I am building a java application that uses the "spark java" framework for the REST API. It is a nice simple framework. But I need to create autogenerated documentation. I found that Swagger is a good thing for that. And I have already used it in my spring boot applications. I found this article:

Swagger and Spark Java integration

It works. But there is a problem. I don't have any dependency injection in my project. In this article is used this construction:

@Api
@Path("/user")
@Produces("application/JSON")
public class CreateUserRoute implements Route {

    @POST
    @ApiOperation(value = "Creates a new user", nickname="CreateUserRoute")
    @ApiImplicitParams({ //
            @ApiImplicitParam(required = true, dataType="string", name="auth", paramType = "header"), //
            @ApiImplicitParam(required = true, dataType = "ro.serol.spark_swagger.route.request.CreateUserRequest", paramType = "body") //
    }) //
    @ApiResponses(value = { //
            @ApiResponse(code = 200, message = "Success", response=User.class), //
            @ApiResponse(code = 400, message = "Invalid input data", response=ApiError.class), //
            @ApiResponse(code = 401, message = "Unauthorized", response=ApiError.class), //
            @ApiResponse(code = 404, message = "User not found", response=ApiError.class) //
    })
    public User handle(@ApiParam(hidden=true) Request request, @ApiParam(hidden=true)Response response) throws Exception {
        return new User();
    }

}

Authour just returns new User();

And he uses reflection for spark route creation(RouteBuilder). But it doesn't work when I use services. Or it might be over complicated(inject services to Route by reflection).

My questions:

  1. How can resolve this dilemma(injections) and use spark java + swagger?
  2. Can someone share a better article/solution for creating API documentation for Spark java?

Upvotes: 1

Views: 1245

Answers (1)

Eric Blue
Eric Blue

Reputation: 11

I realize this is an older post, but wanted to share the solution for this in case others come across this.

That link you shared in your original post from 2016 is no longer available (other than archive.org), but it does give some good hints for getting Swagger working with Spark.

The code examples though where a little verbose don't work with lambda expressions for routes.

I recently migrated a project using the latest version of Spark (2.9.4), and it fully supports Swagger annotations and tested with GET and POST requests. The code for this was just recently pushed to https://github.com/ericblue/spark-starter-basic/

You can define routes in controller classes using the syntax:

get("/api/config", this::testGetConfiguration);

You can see annotation syntax and examples at: https://github.com/ericblue/spark-starter-basic/blob/main/src/main/java/com/ericblue/spark/starter/controllers/api/SampleAPIController.java

The magic of this was made possible from the author of the original post from 2016, but the advantage with these examples are using lambda syntax and not needing to define Routes manually.

Upvotes: 1

Related Questions