美文网首页
SSM配置swagger

SSM配置swagger

作者: wanggs | 来源:发表于2018-08-20 11:34 被阅读0次

    1.引入pom依赖

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

    2.首先当然是去下载swagger,然后引入项目,然后修改index.html里面的url为http://localhost:8080/api-docs

    image.png
    image.png

    3. 新建SwaggerConfig配置类

    package com.wanggs.crm.swagger;
    /**
     * @author Wgs
     * @version 1.0
     * @create:2018/07/13
     */
    @Configuration  //让Spring来加载该类配置
    @EnableWebMvc   //非SpringBoot需启用
    @EnableSwagger2 //启用Swagger2
    public class Swagger2Config {
        static final Logger log= LoggerFactory.getLogger(Swagger2Config.class);
        @Bean
        public Docket createRestApi() {
            log.info("开始加载Swagger2...");
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo()).select()
                    //扫描指定包中的swagger注解
                    //.apis(RequestHandlerSelectors.basePackage("cn.exrick.controller"))
                    //扫描所有有注解的api,用这种方式更灵活
                    .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("XMall Api Documentation")
                    .description("XMall商城管理后台API接口文档")
                    .termsOfServiceUrl("http://blog.exrick.cn")
                    .contact("Exrick")
                    .version("1.0.0")
                    .build();
        }
    }
    

    4. 然后我们在配置类里面配置一下静态资源的映射路径。

    image.png
    使用注解 可以省略
        <bean     class="com.mangofactory.swagger.configuration.SpringSwaggerConfig" />
        <!-- 将自定义的swagger配置类加载到spring容器 -->
        <bean class="org.seckill.config.SwaggerConfig" />
        
        <mvc:resources mapping="/swagger/**" location="/WEB-INF/swagger/"/>
    
        <!-- 定义无需Controller的url<->view直接映射 -->
        <mvc:view-controller path="/" view-name="../WEB-INF/swagger/index"/>
    

    5.编写controller

    package com.wanggs.crm.controller;
    
    /**
     * @author Wgs
     * @version 1.0
     * @create:2018/06/22
     */
    @Api(value = "swagger-hello", description = "demo")
    @RestController
    @RequestMapping()
    public class HelloController {
        @Autowired
        private AccountService accountService;
    
        @ApiOperation(value="crm起步", notes="hello实例")
        @ApiImplicitParam(name = "member", value = "用户详细实体member")
        @GetMapping("/hello")
        public JSONArray show(){
          List<Account> accounts = accountService.findAllAccount();
          for (Account account : accounts){
              System.out.println(account);
          }
            JSONArray jsonArray = (JSONArray) JSONArray.toJSON(accounts);
            return jsonArray;
        }
    }
    
    

    6.测试

    image.png image.png

    相关文章

      网友评论

          本文标题:SSM配置swagger

          本文链接:https://www.haomeiwen.com/subject/hdmkiftx.html