Spring Boot 集成Swagger2生成RESTful API文档

本文详细介绍如何在SpringBoot项目中集成Swagger2,包括添加依赖、配置类编写及控制器注解使用等步骤,并通过示例代码展示如何为RESTful API生成文档。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Swagger2可以在写代码的同时生成对应的RESTful API文档,方便开发人员参考,另外Swagger2也提供了强大的页面测试功能来调试每个RESTful API。

使用Spring Boot可以方便的集成Swagger2

 

1.新建Spring Boot项目

2.添加swagger依赖

<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>

---

3.创建swagger配置类

/**
 * Created by LG on 2017/8/3.
 *
 * Spring Boot中使用Swagger2构建RESTful APIs说明文档
 * 访问http://localhost:8071/swagger-ui.html 查看效果
 */
@Configuration
@EnableSwagger2
public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.luangeng.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs说明文档--title")
                .description("Spring Boot中使用Swagger2构建RESTful APIs说明文档--description")
                .contact("Spring Boot中使用Swagger2构建RESTful APIs说明文档--contact")
                .version("1.0.1")
                .build();
    }

}

---

4.添加一个UserController.java类

@RestController
@RequestMapping(value="/users")
public class UserController {

    private static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());

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

    @ApiOperation(value="创建用户", notes="根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "实体user", required = true, dataType = "User")
    @RequestMapping(value="/", method=RequestMethod.POST)
    public String postUser(@ModelAttribute User user) {
        users.put(user.getId(), user);
        return "success";
    }

    @RequestMapping(value="/{id}", method=RequestMethod.GET)
    public User getUser(@PathVariable Long id) {
        return users.get(id);
    }

    @RequestMapping(value="/{id}", method=RequestMethod.PUT)
    public String putUser(@PathVariable Long id, @ModelAttribute User user) {
        User u = users.get(id);
        u.setName(user.getName());
        u.setAge(user.getAge());
        users.put(id, u);
        return "success";
    }

    @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
    public String deleteUser(@PathVariable Long id) {
        users.remove(id);
        return "success";
    }

}

---

再增加一个InfoController.java类

@RestController
public class InfoController {

    @Autowired
    MyConfig myconfig;

    @RequestMapping("/info")
    public String msg() {
        return "client1 service"+myconfig.toString();
    }

}

---

6.访问http://localhost:8071/swagger-ui.html 查看效果

+

 

 

end

转载于:https://www.cnblogs.com/luangeng/p/6130475.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值