spring boot 整合swagger ui

spring boot 整合swagger ui

1.导入依赖

		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.8.0</version>
		</dependency>

		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.8.0</version>
		</dependency>

2.配置swagger工具类(界面显示,不配置也可以)

@Configuration
public class Swagger2Utils {

    @Bean
    public Docket createRestApi(){
        return  new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.example.test_kj.controller")) //扫描的controller地址
                .paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder().title("springboot整合swagger")
                .description("雨落") //描述
                .termsOfServiceUrl("https://blog.csdn.net/qq_35414330/article/details/82422697") //链接
                .version("1.0").build();
    }

}

3.controller

@Controller
@Api("用户信息")
@RequestMapping("user")
public class UserController {
    @Autowired
    private TestInterFace testInterFace;

    @RequestMapping(value = "/getUserById/{id}",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
//    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "path")
    public User getUser(@PathVariable(value = "id") String userId) {
        return testInterFace.testUser(userId);
    }

    @RequestMapping(value = "/getUserList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取用户列表",notes = "获取用户列表")
    public List<User> getUserList(){
        return testInterFace.getUserList();
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="创建用户", notes="根据User对象创建用户")
//    @ApiImplicitParam(name = "reqData", value = "用户详细实体user", required = true, dataType = "AddUserRequest")
    public String add(@RequestBody AddUserRequest reqData) {
        testInterFace.insertUser(reqData);
        return "插入成功";
    }


    @ApiOperation(value="删除用户", notes="根据url的id来指定删除用户")
//    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "deleteById/{id}",method = RequestMethod.GET)
    @ResponseBody
    public  String deleteById(@PathVariable(value = "id")String id){
        testInterFace.deleteById(id);
        return "删除成功";
    }


}

4.开启swagger2  (@EnableSwagger2)

@SpringBootApplication
@EnableSwagger2  //开启swagger2
public class TestKjApplication {

	public static void main(String[] args) {
		SpringApplication.run(TestKjApplication.class, args);
	}

}

5.启动项目,访问 http://localhost:8080/swagger-ui.html

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值