功能设计
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200301214339102.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2gzNTYzNjM=,size_16,color_FFFFFF,t_70)
代码结构
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200301214457923.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2gzNTYzNjM=,size_16,color_FFFFFF,t_70)
Swagger 文档
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200301214544526.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2gzNTYzNjM=,size_16,color_FFFFFF,t_70)
Maven Pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>bsea</groupId>
<artifactId>springboot2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>springbootblog</artifactId>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--目的:《可选》引入springboot 热启动,每次修改以后,会自动把改动加载,不需要重启服务了-->
<dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!-- <version>8.0.15</version> -->
</dependency>
<!-- 添加JPA的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</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>
</dependencies>
</project>
Swagger 配置文件
package com.xsz.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket buildDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(buildApiInf())
.select()
.apis(RequestHandlerSelectors.basePackage("com.xsz.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo buildApiInf() {
return new ApiInfoBuilder()
.title("系统RESTful API文档")
.contact(new Contact("Bsea", "https://me.csdn.net/h356363", "yinyouhai@aliyun.com"))
.version("1.0")
.build();
}
}
/**
*
*
* Swagger常用注解
@Api:修饰整个类,描述Controller的作用;
@ApiOperation:描述一个类的一个方法,或者说一个接口;
@ApiParam:单个参数描述;
@ApiModel:用对象来接收参数;
@ApiProperty:用对象接收参数时,描述对象的一个字段;
@ApiResponse:HTTP响应其中1个描述;
@ApiResponses:HTTP响应整体描述;
@ApiIgnore:使用该注解忽略这个API;
@ApiError :发生错误返回的信息;
@ApiImplicitParam:一个请求参数;
@ApiImplicitParams:多个请求参数。
编写RESTful API接口
Spring Boot中包含了一些注解,对应于HTTP协议中的方法:
@GetMapping对应HTTP中的GET方法;
@PostMapping对应HTTP中的POST方法;
@PutMapping对应HTTP中的PUT方法;
@DeleteMapping对应HTTP中的DELETE方法;
@PatchMapping对应HTTP中的PATCH方法。
**
http://localhost:9004/blog/swagger-ui.html
**/
文章控制类
package com.xsz.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xsz.entity.Blog;
import com.xsz.service.BlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
/**
*
* @author Bsea
* 文件管理模块 文章控制类
*/
@RequestMapping("article")
@RestController
@Api(value = "文章API")
public class ArticleController {
@Resource
BlogService blogService;
//发表文章
@ApiOperation(value = "发表文章", notes = "发表文章")
@PostMapping("save")
public Blog add(Blog blog) {
return blogService.add(blog);
}
//修改文章
@ApiOperation(value = "修改文章", notes = "修改文章")
@PostMapping("modify")
public Blog update(Blog blog) {
return blogService.update(blog);
}
//删除文章
@ApiOperation(value = "删除文章", notes = "删除文章")
@ApiImplicitParam(name = "id", value = "文章的主键Id", required = true, dataType = "String", paramType = "path")
@PostMapping("delete/{id}")
public Map<String, String> delete(@PathVariable("id") String id) {
Map<String, String> result=new HashMap();
blogService.delete(id);
result.put("result", "success");
return result;
}
//根据作者查询文章
@ApiOperation(value = "根据作者查询文章", notes = "根据作者查询文章")
@ApiImplicitParam(name = "id", value = "作者的主键Id", required = true, dataType = "String", paramType = "path")
@GetMapping("showByAuthor/{id}")
public List<Blog> selectByAuthor(@PathVariable("id") String id) {
return blogService.selectByAuthor(id);
}
//根据标题查询文章
@ApiOperation(value = "根据标题查询文章", notes = "根据标题查询文章")
@ApiImplicitParam(name = "title", value = "标题", required = true, dataType = "String", paramType = "path")
@GetMapping("showByTitle/{title}")
public Blog selectByTitle(@PathVariable("title") String title) {
return blogService.selectByTitle(title);
}
}