Swagger
Swagger可以很方便的直接生成项目的接口,便于前后端的分离式开发,并且它还具备调试等功能,可以说十分方便。以这篇文章记录一些Swagger在Springboot项目开发中的使用。
1. 引入依赖
<!-- swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
2.Swagger接口注解
2.1 实体类
@ApiModel为实体类的解释说明
@ApiModel("User实体类")
@Data
@Repository
public class User {
@ApiModelProperty("用户的ID")
private String userName;
private String password;
private User user;
}
2.2 Controller层
@Api为Controller类的解释说明
@ApiOperation为Controller类方法的解释说明
@Api("HelloController")
@RestController
public class HelloController {
@ApiOperation("hello控制类")
@RequestMapping(method = RequestMethod.GET, value = "/api/javainuse")
public String sayHello() {
return "Swagger Hello World";
}
@ApiOperation("测试登录")
@RequestMapping(method = RequestMethod.POST,value = "/api/testLogin")
public User send(String username, String password){
User user = new User();
User user1 = new User();
user.setUserName(username);
user.setPassword(password);
user1.setUserName(username);
user1.setPassword(password);
user.setUser(user1);
return user;
}
}
3. Swagger配置层
在这里进行SwaggerUI界面的配置,显示一些相关信息。
@Configuration
@EnableSwagger2
public class SpringFoxConfig {
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.build();
}
//配置Swagger的信息 apiInfo
private ApiInfo apiInfo(){
//作者信息
Contact contact = new Contact("yao","none","1482089698@qq.com");
return new ApiInfo(
"SwaggerDemo",
"这是一个DEMO",
"1.0",
"http://localhost:8080",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<VendorExtension>());
}
}
4. 显示接口的地址
http://localhost:8080/swagger-ui.html
(ps:这里的8080是你的项目默认启动端口,如果非8080可以自行修改)
5. 显示界面
其它技术文章
https://halo-2g7yx69u1c372d6f-1311471022.ap-shanghai.app.tcloudbase.com/archives/swagger%E2%80%93zi-dong-sheng-cheng-jie-kou-wen-dang
相关文章
暂无评论...