Swagger UI将API信息可视化,任何人可以查看api信息并进行交互
1. pom.xml 添加依赖
<!-- https://mvnrepository.com/artifact/io.swagger/swagger-models -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.22</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.swagger/swagger-annotations -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.22</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-bean-validators -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
<version>2.9.2</version>
</dependency>
2. 配置类
@Configuration //需要添加这个
@EnableSwagger2 //需要添加这个
public class mySwaggerConfig{
}
package com.example.questionlist2.cofig;
import com.google.common.collect.Lists;
import net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.PathContainer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Swagger 引入, 自己的swagger配置类
*/
@Configuration
@EnableSwagger2
public class mySwaggerConfig {
@Bean
public Docket docket(){
ParameterBuilder builder = new ParameterBuilder();
builder.parameterType("header").name("myKey")
.description("My Swagger try try try")
.required(true)
.modelRef(new ModelRef("String")); //swagger中展示header
return new Docket(DocumentationType.SWAGGER_2)
.groupName("my swagger try")
.apiInfo(apiinfo())
.globalOperationParameters(Lists.newArrayList(builder.build()))
.select().paths(PathSelectors.any()).build();
}
private ApiInfo apiinfo(){
return new ApiInfoBuilder()
.title("questionSystem")
.description("question 接口文档")
.contact(new Contact("163email","","windelubnu@163.com"))
.version("1.0")
.build();
}
}
3. 常用注解
重启服务, 并进入页面http://localhost:7777/swagger-ui.html#/basic-error-controller
## 1). @Api("getquestion") @ApiOperation("跟service相关")
@Api("getquestion")
@RestController //Spring会转换返回值并将日写入http相应
//@RequestMapping("get") //用于类和防范,在方法级别时, 处理http的各种防范
public class getListController {
@Autowired //向这个bean中注入其他bean questionServiceImpl
private questionServiceImpl questionServiceimpl;
@ApiOperation("跟service相关")
@PostMapping("getquestion4impl")
public String getQuestion3(@RequestBody questionDto q){
return "getquestion4impl >>"+ questionServiceimpl.getQuestion3(q);
}
@Value("${config.key}") //如果没有${} 就会将config.key 赋值给key
private String key;
@GetMapping("get")
@ApiOperation("跟配置相关")
public String get(){
return "hello get"+key;
}
2). @ApiModel @ApiModelProperty
package com.example.questionlist2.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(value = "Question 类",description = "请求类")
public class questionDto {
@ApiModelProperty(value="question 类型",example = "type1",required = true)
private String QuestionType;
private String keyword;
@ApiModelProperty(value="question 名称",example = "name1",required = true)
private String questionName;
private String dependOnQuestion;
private String book;
// public String getQuestionType() {
// return QuestionType;
// }
//
// public void setQuestionType(String questionType) {
// QuestionType = questionType;
// }
//
// public String getKeyword() {
// return keyword;
// }
//
// public void setKeyword(String keyword) {
// this.keyword = keyword;
// }
//
// public String getQuestionName() {
// return questionName;
// }
//
// public void setQuestionName(String questionName) {
// this.questionName = questionName;
// }
//
// public String getDependOnQuestion() {
// return dependOnQuestion;
// }
//
// public void setDependOnQuestion(String dependOnQuestion) {
// this.dependOnQuestion = dependOnQuestion;
// }
//
// public String getBook() {
// return book;
// }
//
// public void setBook(String book) {
// this.book = book;
// }
public questionDto(String questionType, String keyword, String questionName, String dependOnQuestion, String book) {
this.QuestionType = questionType;
this.keyword = keyword;
this.questionName = questionName;
this.dependOnQuestion = dependOnQuestion;
this.book = book;
}
}