How to describe REST api using Spring?

I use spring-mvc to create servlets such as:

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

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

Question: is this possible with any spring framework like HATEOAS ?

I would expect the framework to automatically detect any @RestController and included methods.

Or do I need to create this review page?

+5
source share
3 answers

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

Configuring Swagger 2 using the Spring REST API

You can also create a swagger definition for your leisure apis that clients can use to create client classes.

Also swagger ui can be used to test / call your APIs. swagger provides a user interface in which you can enter all api inputs, such as request parameters, path parameters, request body, headers.

Swagger UI Example

+2
source

You should look into this

To integrate it into spring, you can reference this

Swagger is one of the best frameworks for exposing RESTful APIs.

+3
source

You can check out this Spring Restdocs ( github ) project, which allows you to create ready-to-use REST documents. It is officially supported by the Spring team:

The main goal of this project is to simplify the documentation of RESTful Services by combining content that was manually written by Asciidoctor with auto-generated examples created using the Spring MVC Test Framework. The result should be easy to read by the user. a manual similar to the GitHub API documentation , rather than a fully automated, tight API documentation created by tools such as Swagger .

Another option is to use Swagger , it supports an upstream approach:

An upstream approach in which 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 use one of the supported frameworks (JAX-RS, node.js, etc.), you can get the Swagger definition automatically created for you .

Here are some examples of reversals: 1 2

+1
source

Source: https://habr.com/ru/post/1268802/


All Articles