Simple example of Springboot integration Swagger2

Source: Internet
Author: User
Tags web services

Swagger is a normative and complete framework for generating, describing, invoking, and visualizing RESTful Web services. The overall goal is to have the client and the file system update at the same speed as the server. File methods, parameters and models are tightly integrated into the server-side code, allowing the API to always stay in sync. Swagger making deployment management and using powerful APIs has never been easier.

Here I bring you a simple integration demo first look at the project structure

Here are the basic steps

I. Adding MAVEN dependencies

  1. <dependency>
  2. <groupId>io.springfox</groupId>
  3. <artifactId>springfox-swagger2</artifactId>
  4. <version>2.2.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>io.springfox</groupId>
  8. <artifactId>springfox-swagger-ui</artifactId>
  9. <version>2.2.2</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.codehaus.jackson</groupId>
  13. <artifactId>jackson-core-asl</artifactId>
  14. <version>1.9.13</version>
  15. </dependency>

Two. Writing the Swagger configuration class

  1. @Configuration
  2. @EnableSwagger2
  3. Public class Swaggers {
  4. @Bean
  5. Public Docket Swaggerspringmvcplugin () {
  6. Apiinfo apiinfo = new Apiinfo ("Sample of Springboot", "sample of Springboot", null, null, null, null, null);
  7. Docket Docket = new Docket (documentationtype.swagger_2). Select (). Paths (Regex ("/user/.*")). Build ()
  8. . Apiinfo (Apiinfo). Usedefaultresponsemessages (false);
  9. return docket;
  10. }
  11. /*private apiinfo apiinfo () {
  12. return new Apiinfobuilder (). Title ("Test API")
  13. . Description ("Shian Test API1")
  14. . Version ("1.0.0")
  15. . build ();
  16. }*/
  17. / * @Bean
  18. Public Docket Createrestapi () {
  19. return new Docket (documentationtype.swagger_2)
  20. . Apiinfo (Apiinfo ())
  21. . Select ()
  22. . APIs (Requesthandlerselectors.basepackage ("Com.didispace.web"))
  23. . Paths (Regex ("/user/.*"))
  24. . build ();
  25. }
  26. */
  27. }

Of course, it can be implemented in a chained programming way, where I use new

Three. Writing a controller

  1. @RestController
  2. @RequestMapping ("/user")
  3. @Api (value = "Shop")
  4. Public class Springbootcontroller {
  5. @ApiOperation (value = "Get HelloWorld", notes = "simple SPRINGMVC Request")
  6. @RequestMapping ("/")
  7. String Home () {
  8. return "HELLO World";
  9. }
  10. @ApiOperation (value = "Get A+b", notes = "Add a string of request parameters based on the URL's classno and URL studentname, restful style request")
  11. @ApiImplicitParams ({@ApiImplicitParam (name = "Classno", value = "class number", Required = true, DataType = c8> "String"),
  12. })
  13. @RequestMapping (value = "/class/{classno}/to/{studentname}", method = Requestmethod.get)
  14. String World (@PathVariable ("Classno") string Classno, @PathVariable ("Studentname") string Studentname) {
  15. return Classno + "" + studentname;
  16. }
  17. }

Four. Writing application loading class


    1. @SpringBootApplication
    2. Public class Application {
    3. public static void Main (string[] args) {
    4. Springapplication.run (Application.   Class,args);
    5. }
    6. }

Swagger will default to all controllers in the Requestmapping method generated API out, in fact, we generally only need the standard interface (like the controller method returned to the page we do not need), All you can do is to set the requirements for the method to generate the API as follows.

This function is basically implemented, we can access the address http://localhost:8080/swagger-ui.html/

View the generated API

Simple example of Springboot integration Swagger2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.