rest interface configuration swagger to spring boot in writing Controller

1.pom.xml file add the following dependencies:

<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>
2.创建一个Swagger2类:
// configuration of swagger 
@Configuration
@ EnableSwagger2
public class Swagger2 {
@Bean
public Docket No. createRestApi () {
return new new Docket No. (DocumentationType.SWAGGER_2)
.apiInfo (apiInfo ())
.Select ()
.apis (RequestHandlerSelectors.basePackage ( "COM .example.demo ")) // scan interface package
.paths (PathSelectors.regex (" / rest /.*"))// address of the interface is displayed documents, only show here at rest / address
.build ( );
}

public apiInfo apiInfo () {
return new new ApiInfoBuilder ()
.title ( "Boot Spring swagger constructed using api documentation")
.description ( "rest simple and elegant style")
.termsOfServiceUrl ( "http://www.maycpou.com") // show the URL of the document development agreement followed
.version ( "1.0") // version
.build ();
}
}
access 3. After starting the project http: //localhost:8888/swagger-ui.html
4. for swagger2 there are many notes written in the inbound and outbound interfaces or above parameters, to add in the description of swagger-document interface increases the interface or parameters such as:
interface method :
@ApiOperation (value = "Add article", notes = "Add new article", tags = "Article", httpMethod = "POST") // swagger annotation methods 
// @ApiImplicitParams ({
// @ApiImplicitParam (name = "id", value = "article Id", required = true, dataType = "String") ,
// @ApiImplicitParam (name = "name", value = "article name", required = true, dataType = "String") ,
//}) // this is a request for annotation parameter is time @RequestParam embodiment, the annotations on the document can be added for each parameter swagger
@ApiResponses ({// method returns swagger annotation values
@ApiResponse (code = 200, message = "success", Response = AjaxResponse.class),
@ApiResponse (code = 400, message = "user input error", Response = AjaxResponse.class),
@ApiResponse (code = 500, message = "Internal system error ", Response = AjaxResponse.class)
})
@RequestMapping (value = "/ Article This article was", Method = RequestMethod.POST, Produces = "file application / JSON")
// equivalent to the above comments @PostMapping ( "/ Article This article was")
public AjaxResponse saveArticle (Article This article was Article This article was @RequestBody) {/ / reception parameters used @RequestBody manner, can be automatically converted incoming assembly json object
// If we must accept parameters @RequestParam manner inside the Article object fields in the incoming write all parameters
log.info ( "saveArticle: {}", Article This article was);
return AjaxResponse.success ();
}

outgoing parameters:
@Data 
@ @ ApiModel interface for swagger return entities
public class AjaxResponse {
@ApiModelProperty ( "success") // Returns the comment display field in the swagger
Private Boolean IsOK;
Private int code;
Private String Message;
Private Data Object ;
Private AjaxResponse () {

}

public static AjaxResponse Success () {
AjaxResponse resultBean new new AjaxResponse = ();
resultBean.setIsok (to true);
resultBean.setCode (200 is);
resultBean.setMessage ( "Success");
return resultBean;
}

public Success AjaxResponse static (Object Data) {
AjaxResponse resultBean new new AjaxResponse = ();
resultBean.setIsok(true);
resultBean.setCode(200);
resultBean.setMessage("success");
resultBean.setData(data);
return resultBean;
}
}


Guess you like

Origin www.cnblogs.com/maycpou/p/11621807.html