Springboot integrates swagger2 and springbootswagger2

Source: Internet
Author: User

Springboot integrates swagger2 and springbootswagger2

Introduction:

Swagger is the world's largest OpenAPI specification (OAS) API development tool framework, supporting the development of the entire API lifecycle from design and documentation to testing and deployment. (From the Swagger official website) Swagger says that it helps developers save time to maintain the interface documentation and is very convenient to debug interfaces.

1. Create a project

In the previous blog, I introduced how to create the springboot project. In this blog, I will only talk about how to create a springboot project. At the end of the blog, I will upload the source code of the project. The friends who love to learn can download and communicate with each other, if you have any questions, you can leave a message at the bottom of the blog. The blogger will reply to you immediately after seeing it.

Ii. Introduce swagger2 dependency

 

    <!--swagger2-->        <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>            <version>2.6.1</version>        </dependency>

3. Create a Swagger2 configuration class

Package com. chaoqi. springboot_swagger.dao.utils; import org. springframework. context. annotation. bean; import org. springframework. context. annotation. configuration; import springfox.doc umentation. builders. apiInfoBuilder; import springfox.doc umentation. builders. pathSelectors; import springfox.doc umentation. builders. requestHandlerSelectors; import springfox.doc umentation. service. apiInfo; import springfox.doc umentation. spi. documentationType; import springfox.doc umentation. spring. web. plugins. docket; import springfox.doc umentation. swagger2.annotations. enableSwagger2; // Let Spring load the class Configuration @ Configuration // enable Swagger2 @ EnableSwagger2public class Swagger2 {@ Bean public Docket createRestApi () {return new Docket (DocumentationType. SWAGGER_2 ). groupName ("HelloWorld "). apiInfo ()). select () // The Position of the scanned package. apis (RequestHandlerSelectors. basePackage ("com. chaoqi. springboot_swagger.web ") // scan URL rules. paths (PathSelectors. any ()). build ();} private ApiInfo apiInfo () {// contact information return new ApiInfoBuilder () // large title. title ("") // description. description ("") // Terms of Service URL. termsOfServiceUrl ("") // version. version (""). build ();}}

Create a controller

Swagger2 Annotations:

@ Api: Used in the request class. It indicates the description of the class. tags = "indicates the role of the class. The annotation" value = "is meaningless on the UI interface, you can also see it on the UI, so you do not need to configure "@ ApiOperation: Used in the request method, description of the purpose and purpose of the method value = "description of the purpose of the method, role" notes = "description of the method" @ ApiImplicitParams: Used in the request method, indicates a set of parameter description @ ApiImplicitParam: Used in the @ ApiImplicitParams annotation to specify all aspects of a request parameter name: parameter name value: parameter description of Chinese characters and interpretation of required: whether the parameter must be passed paramType: where the parameter is placed · header --> request parameter acquisition: @ RequestHeader · query --> request parameter acquisition: @ RequestParam · path (for restful interfaces) --> request parameter acquisition: @ PathVariable · body (not commonly used) · form (not commonly used) dataType: parameter type, default String, other values dataType = "Integer" defaultValue: the default value of the parameter @ ApiResponses: used in the request method to indicate a group of responses @ ApiResponse: Used in @ ApiResponses. It is generally used to express an error response message code: Number, for example, 400 message: information. For example, "the request parameter is not filled" response: the class that throws an exception @ ApiModel: is used in the response class to indicate the information of a returned response data (this is generally used when post is created, in scenarios such as @ RequestBody, when the request parameter cannot be described using the @ ApiImplicitParam annotation) @ ApiModelProperty: Describes the attributes of the response class.

 

Package com. chaoqi. springboot_swagger.web; import com. chaoqi. springboot_swagger.dao.domain.MusicInfo; import com. chaoqi. springboot_swagger.service.MusicInfoService; import io. swagger. annotations. api; import io. swagger. annotations. apiImplicitParam; import io. swagger. annotations. apiImplicitParams; import io. swagger. annotations. apiOperation; import org. springframework. beans. factory. annotation. autowired; import org. springframework. web. bind. annotation. *; import java. util. list; @ RestController @ RequestMapping (value = "/music") @ Api (tags = "music Info") public class MusicInfoController {@ Autowired private MusicInfoService musicInfoService; @ ApiOperation (value = "obtain artist information by Id") @ ApiImplicitParams ({@ ApiImplicitParam (name = "id", value = "User Id", required = true, paramType = "query", dataType = "Long"),}) @ RequestMapping (value = "/showMusic", method = RequestMethod. GET) public List <MusicInfo> getMusicInfo (@ RequestParam (name = "id", required = false) Long id) {List <MusicInfo> musicInfo1 = musicInfoService. getMusicInfo (id); return musicInfo1 ;}@ ApiOperation (value = "") @ ApiImplicitParam (name = "Id", value = "User id ", required = true, paramType = "query", dataType = "Long") @ RequestMapping (value = "/deleteMusic", method = RequestMethod. DELETE) public Long deleteUser (@ RequestParam (name = "id", required = false) Long id) {Long sum = musicInfoService. getDeleteId (id); return sum ;}}

Start the project to run http: // localhost: 8080/swagger-ui.html

Integration successful, source: https://github.com/caicahoqi/ChaoqiIsPrivateLibrary

Related Article

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.