springmvc集成swagger使用
程序员文章站
2024-03-02 17:09:34
...
引入依赖
!-- 构建Restful API -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
编写swagger配置类
package com.port.socket.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration //让Spring来加载该类配置
@EnableWebMvc //启用Mvc,非springboot框架需要引入注解@EnableWebMvc
@EnableSwagger2 //启用Swagger2
@ComponentScan(basePackages = {"com.port.socket"}) // 扫描路径
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()).select()
//springboot扫描指定包中的swagger注解
//.apis(RequestHandlerSelectors.basePackage("com.xia.controller"))
//springboot扫描所有有注解的api,用这种方式更灵活
// .apis(RequestHandlerSelectors.basePackage("com.port"))
// .paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("swagger测试")
.description("基础平台 RESTful 风格的接口文档,内容详细,极大的减少了前后端的沟通成本,同时确保代码与文档保持高度一致,极大的减少维护文档的时间。")
.termsOfServiceUrl("http://localhost:8888/test/swagger.html")
.contact("aaa@qq.com")
.version("1.0.0")
.build();
}
}
实体类
package com.port.socket.bean;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
@ApiModel(value = "user对象", description = "用户对象user")
public class User implements Serializable {
@ApiModelProperty(value = "用户名", name = "username", example = "admin")
private String username;
@ApiModelProperty(value = "密码", name = "password", example = "123")
private String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
控制层
package com.port.socket.controller;
import com.port.socket.bean.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("/test")
@Api(value = "API TestController", tags = { "测试管理" })
public class TestController {
@ApiOperation(value = "测试查找",notes = "get")
@RequestMapping(value = "/get",method = RequestMethod.GET)
@ResponseBody
public Map get(){
return new HashMap();
}
@ApiOperation(value = "测试添加",notes = "post")
@ApiImplicitParams({//参数列表
@ApiImplicitParam(name = "user",value = "用户信息", required = true, paramType = "user"),
//@ApiImplicitParam(name = "request",value = "request", required = true, paramType = "query")
})
@RequestMapping(value = "/post",method = RequestMethod.POST)
@ResponseBody
public Map post(User user){
return new HashMap();
}
@ApiOperation(value = "测试更新",notes = "put")
@ApiImplicitParams({//参数列表
@ApiImplicitParam(name = "user",value = "用户信息", required = true, paramType = "user"),
//@ApiImplicitParam(name = "request",value = "request", required = true, paramType = "query")
})
@RequestMapping(value = "/put",method = RequestMethod.PUT)
@ResponseBody
public Map put(User user){
return new HashMap();
}
@ApiOperation(value = "测试删除",notes = "delete")
@ApiImplicitParams({//参数列表
@ApiImplicitParam(name = "id",value = "用户id", required = true, paramType = "String"),
//@ApiImplicitParam(name = "request",value = "request", required = true, paramType = "query")
})
@RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
@ResponseBody
public Map delete(@PathVariable String id){
return new HashMap();
}
}
访问方式
http://ip:端口号/项目名/swagger-ui.html
效果
源码地址自己下载sql在webapp下
不能使用请留言
https://github.com/Houshixin/demo