Swagger, Chinese for “ye”. It is a powerful API framework, and its integration is very simple, not only provides online document lookup, but also provides online document testing. In addition, Swagger is easy to build restful apis, simple and elegant, as its name suggests.

First, introduce dependencies

< the dependency > < groupId > IO. Springfox < / groupId > < artifactId > springfox - swagger2 < / artifactId > < version > 2.6.1 < / version > </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> The < version > 2.6.1 < / version > < / dependency >Copy the code

Write configuration classes

@Configuration
@EnableSwagger2
public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.forezp.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Springboot builds API documents with Swagger")
                .description("Simple and elegant style of restfun, http://blog.csdn.net/forezp")
                .termsOfServiceUrl("http://blog.csdn.net/forezp")
                .version("1.0") .build(); }}Copy the code

The @Configuration annotation indicates that it is a Configuration class and @enablesWagger2 enables Swagger2. ApiINfo () configures some basic information. Apis () specify that scanned packages generate documentation.

Write notes for production documents

Swagger annotates that the interface generates documentation, including the interface name, request method, parameters, return information, and so on.

  • @Api: Decorates the entire class to describe what Controller does
  • ApiOperation: Describes a method, or interface, of a class
  • @APIParAM: Single parameter description
  • @APIModel: Accept arguments with objects
  • ApiProperty: A field describing an object when receiving a parameter with the object
  • @apiResponse: HTTP responds to one of the descriptions
  • @apiresponses: Overall description of HTTP responses
  • @apiIgnore: Ignore the API with this annotation
  • @apiError: Information returned when an error occurs
  • @APIParamIMPLICITL: A request parameter
  • @apiParamSimplicit Multiple request parameters

Now through a chestnut:

package com.forezp.controller; import com.forezp.entity.Book; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.*; import springfox.documentation.annotations.ApiIgnore; import java.util.*; /** * The user creates a book POST /books/ * The user modifies a book PUT /books/:id/ * The user deletes a book DELETE /books/:id/ * The user obtains all books GET /books * The user obtains a book Id * Created by fangzhipeng on 2017/4/17. http://swagger.io/docs/specification/api-host-and-base-path/ */ @RestController @RequestMapping(value ="/books")
public class BookContrller {

    Map<Long, Book> books = Collections.synchronizedMap(new HashMap<Long, Book>());

    @ApiOperation(value="Get book List", notes="Get book List")
    @RequestMapping(value={""}, method= RequestMethod.GET)
    public List<Book> getBook() {
        List<Book> book = new ArrayList<>(books.values());
        return book;
    }

    @ApiOperation(value="Create a book", notes="Create a book")
    @ApiImplicitParam(name = "book", value = "Book Detailed entity", required = true, dataType = "Book")
    @RequestMapping(value="", method=RequestMethod.POST)
    public String postBook(@RequestBody Book book) {
        books.put(book.getId(), book);
        return "success";
    }
    @ApiOperation(value="Access to Book Details", notes="Get details based on the ID of the URL")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long",paramType = "path")
    @RequestMapping(value="/{id}", method=RequestMethod.GET)
    public Book getBook(@PathVariable Long id) {
        return books.get(id);
    }

    @ApiOperation(value="Update information", notes="Update book information specified by URL ID")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "The book ID", required = true, dataType = "Long",paramType = "path"),
            @ApiImplicitParam(name = "book", value = "Physical book", required = true, dataType = "Book")
    })
    @RequestMapping(value="/{id}", method= RequestMethod.PUT)
    public String putUser(@PathVariable Long id, @RequestBody Book book) {
        Book book1 = books.get(id);
        book1.setName(book.getName());
        book1.setPrice(book.getPrice());
        books.put(id, book1);
        return "success";
    }
    @ApiOperation(value="Delete books", notes="Delete books by URL ID")
    @ApiImplicitParam(name = "id", value = "The book ID", required = true, dataType = "Long",paramType = "path")
    @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
    public String deleteUser(@PathVariable Long id) {
        books.remove(id);
        return "success"; } @apiignore // Use this annotation to ignore the API @requestMapping (value ="/hi", method = RequestMethod.GET)
    public String  jsonTest() {
        return " hi you!"; }}Copy the code

SpringCloud large enterprise distributed micro service cloud to build B2B2C e-commerce platform source code please add penguin beg: 10387746626