exam在线考试系统整合swagger

本文档介绍了如何将swagger集成到exam在线考试系统中,以简化接口文档的编写和提高沟通效率。主要步骤包括在pom.xml添加依赖,创建配置类并启用Swagger2,配置资源放行,以及在特定控制器类上进行设置。完成这些步骤后,通过http://ip:端口号/swagger-ui.html可以访问接口文档。

我们在开发过程中,经常需要和别人交流项目,经常和别人对接的时候,就需要我们去写接口文档,如果单纯的是使用手写的方式比较麻烦,而且也费时间,所以说呢,为了方便,今天就把swagger整合到在线考试系统(exam)里面,使用起来很爽; 避免接口文档的书写。

整合的步骤也比较简单, 需要注意的就是需要资源放行一下。下面就来说说整合的步骤
(1) 先在项目的pom.xml里面加入依赖

 <!-- swagger2-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

(2) 添加一个配置类,需要注意的是要添加 @Configuration 和 @EnableSwagger2注解

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.soulcoder.exam.web.controller"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                        .title("Exam整合Swagger2")
                        .description("在线考试系统集成swagger2")
                        .version("9.0")
                        .license("xxxxx")
                        .licenseUrl("www.xxxx.com")
                        .build());
    }
}

(3)记住资源放行

		registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

(4) 在类上面进行配置
比如 UserController类

@Controller
@Api(tags = "在线考试系统-用户管理接口")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IRoleService roleService;

    @GetMapping("/user/index")
    @ApiOperation("跳转用户页接口")
    @PreAuthorize("hasRole('管理员')")
    public String index(String menuid,Model model){
        List<Role> roles = queryAllRole();
        model.addAttribute("roles",roles);
        model.addAttribute("menuid",menuid);
        //用户首页
        return "views/user/user_list";
    }
  }

(5)启动项目 输入地址
http://ip:端口号/swagger-ui.html 进行访问
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值