代码拉取完成,页面将自动刷新
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
@Configuration
@EnableSwagger2
public class Swagger2Configuration {
@Bean
public Docket buildDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.xxx.web.controller")) //要扫描的API(Controller)基础包
.paths(PathSelectors.any()) // and by paths
.build()
.apiInfo(buildApiInf());
}
private ApiInfo buildApiInf() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2 UI构建API文档")
.contact("test")
.version("1.0.0")
.build();
}
}
@RestController
@RequestMapping("article")
@Api(value = "文章服务",description="提供 增删改查 API")
public class ArticleController {
@Autowired
private ArticleService articleServiceImpl;
@PostMapping
@ApiOperation("添加")
public WebResult save(@RequestBody Article article) {
return WebResult.ok(articleServiceImpl.save(article));
}
@PutMapping
@ApiOperation("修改")
public WebResult update(@RequestBody Article article) {
return WebResult.ok(articleServiceImpl.update(article));
}
@DeleteMapping("{id}")
@ApiOperation("删除")
public WebResult delete(@ApiParam("文章id") @PathVariable String id) {
return WebResult.ok(articleServiceImpl.remove(id));
}
@GetMapping("{id}")
@ApiOperation("查找")
public WebResult get(@ApiParam("文章id") @PathVariable String id) {
return WebResult.ok(articleServiceImpl.get(id));
}
}
@Entity
@ApiModel("Article(文章实体)")
public class Article extends BaseEntity {
/**
* 标题
*/
@ApiModelProperty("标题")
private String title;
/**
* 正文
*/
@Type(type = "text")
@ApiModelProperty("正文")
private String content;
/**
* 发表时间
*/
@JsonFormat(pattern = "yyyy/MM/dd")
@ApiModelProperty("发表时间")
private LocalDateTime publishedTime;
/**
* 作者
*/
@ApiModelProperty("作者")
private String author ;
public Article() {
this.publishedTime = LocalDateTime.now();
}
public Article(String title , String content) {
this.title = title ;
this.content = content ;
this.publishedTime = LocalDateTime.now();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public LocalDateTime getPublishedTime() {
return publishedTime;
}
public void setPublishedTime(LocalDateTime publishedTime) {
this.publishedTime = publishedTime;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
重新启动下Application,访问 http://localhost:8080/swagger-ui.html (“项目访问链接”+“/swagger-ui.html”)。即可看到 swagger 生成的 API文档。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。