How to self describe a REST api with Spring?
Asked Answered
C

3

5

I'm using spring-mvc to create servlets like:

@RestController
public class MyServlet {
   @GetMapping("/test")
   public MyRsp test(MyReq req) {
         //...
   }
}

Now if the user accesses the root of my app localhost:8080/my-app, it should show a list of GET and POST methods available. At best with possible input parameters, acceptable headers etc.

Question: is that possible with any spring framework, like HATEOAS?

I'd expect a framework to auto detect any @RestController and included methods.

Or would I have to create that overview page myself?

Catinacation answered 13/6, 2017 at 10:18 Comment(0)
E
3

Swagger 2 is an another option. read the following to know more about swagger and how to set it up.

Setting Up Swagger 2 with a Spring REST API

You can also create swagger definition for your rest apis, which can be used by the clients to generate client classes.

Also the swagger ui can be used to test/invoke your APIs. swagger provides a user interface where you can input all the api inputs such as query params, path params, request body, headers.

Sample Swagger UI

Escurial answered 13/6, 2017 at 10:27 Comment(0)
R
4

You should must look into this

To integrate it in spring you can refer to this

Swagger is one of the best frameworks to expose RESTful APIs.

Reproach answered 13/6, 2017 at 10:28 Comment(0)
E
3

Swagger 2 is an another option. read the following to know more about swagger and how to set it up.

Setting Up Swagger 2 with a Spring REST API

You can also create swagger definition for your rest apis, which can be used by the clients to generate client classes.

Also the swagger ui can be used to test/invoke your APIs. swagger provides a user interface where you can input all the api inputs such as query params, path params, request body, headers.

Sample Swagger UI

Escurial answered 13/6, 2017 at 10:27 Comment(0)
R
2

You can check this project Spring Restdocs (github), which allows you to generate ready to use REST documentation. It's officially maintained by Spring Team:

The primary goal of this project is to make it easy to document RESTful services by combining content that's been hand-written using Asciidoctor with auto-generated examples produced with the Spring MVC Test framework. The result is intended to be an easy-to-read user guide, akin to GitHub's API documentation for example, rather than the fully automated, dense API documentation produced by tools like Swagger.

The other option is to use Swagger, it supports bottom-up approach as well:

A bottom-up approach where you have an existing REST API for which you want to create a Swagger definition. Either you create the definition manually (using the same Swagger Editor mentioned above), or if you are using one of the supported frameworks (JAX-RS, node.js, etc), you can get the Swagger definition generated automatically for you.

Some examples of swagger are mentioned here: 1 2

Rely answered 13/6, 2017 at 10:22 Comment(2)
Thanks for the hint. But writing test for documentation purpose is not my intention. I'd be happy to just show an overview of available GET/POST methods, alongside with possible parameters and http headers.Catinacation
you're welcome. In such case you can look for "Swagger" swagger.ioRely

© 2022 - 2024 — McMap. All rights reserved.