springMVC 与 Swagger 项目整合

整合步骤:

  1. 添加 swagger 与 springMVC 的依赖 jar 包;
  2. 编写 Swagger 与 SpringMVC 整合的配置类;
  3. 修改配置文件:

                (1)  在 SpringMVC 的扫描注解里面加上扫描注解 @Configuration  ;

                (2)  对称地,要在 spring 配置文件中减去对 @Configuration 的扫描;

    4.  编写 Controller ;

    5. 访问 swagger-api 路径:http://localhost:8080/项目名/swagger-ui.html

具体实现:

1. 添加 Swagger 与 SpringMVC 的依赖jar包

                   <dependency>

                            <groupId>org.springframework</groupId>

                            <artifactId>spring-webmvc</artifactId>

                            <version>4.3.10.RELEASE</version>

                   </dependency>

                   <dependency>

                            <groupId>com.fasterxml.jackson.core</groupId>

                            <artifactId>jackson-databind</artifactId>

                            <version>2.8.7</version>

                   </dependency>

                   <dependency>

                            <groupId>io.springfox</groupId>

                            <artifactId>springfox-swagger-ui</artifactId>

                            <version>2.6.1</version>

                   </dependency>

                   <dependency>

                            <groupId>io.springfox</groupId>

                            <artifactId>springfox-swagger2</artifactId>

                            <version>2.6.1</version>

                   </dependency>

注:出现不能自动解析构建实体类测试,多为导入依赖错误。


2. 编写 Swagger 与 SpringMVC 整合的配置类

/**

 * Swagger2配置类

 * 在与spring boot集成时,放在与Application.java同级的目录下。

 * 通过@Configuration注解,让Spring来加载该类配置。

 * 再通过@EnableSwagger2注解来启用Swagger2。

 */

@Configuration

@EnableWebMvc

@EnableSwagger2

public class SwaggerConfig {

        

          /**

     * 创建API应用

     * apiInfo() 增加API相关信息

     * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,

     * 本例采用指定扫描的包路径来定义指定要建立API的目录。

     *

     * @return

     */

    @Bean

    public Docket petApi() {

        return new Docket(DocumentationType.SWAGGER_2)

                           .apiInfo(apiInfo())

                           .select()

                           .paths(PathSelectors.any())

                           .build();

    }

   

    /**

     * 创建该API的基本信息(这些基本信息会展现在文档页面中)

     * 访问地址:http://项目实际地址/swagger-ui.html

     * @return

     */

    private ApiInfo apiInfo() {

        return new ApiInfoBuilder()

                           .title("服务平台 API")

                           .description("更多请关注http://www.baidu.com")

                           .termsOfServiceUrl("http://www.baidu.com")

                           .version("1.0")

                           .build();

    }

}

 

3. 修改配置文件

Springmvc.xml

<context:component-scan base-package="com.greatmap.swagger" use-default-filters="true">

           <context:include-filter type="annotation" expression="org.springframework.context.annotation.Configuration" />

           <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

      </context:component-scan>

 

      <mvc:default-servlet-handler />

      <mvc:annotation-driven />

 

4. 编写Controller

@Api(value = "Api控制器")

@RestController

@RequestMapping("/api")

publicclass UserController {

 

     private UserService userService = new UserService();

 

     publicvoid setUserService(UserService userService) {

         this.userService = userService;

     }

 

     @GetMapping("/getId")

     @ResponseBody

     @ApiOperation(value = "获取用户信息", notes = "根据用户id获取用户信息")

     @ApiImplicitParams({

              @ApiImplicitParam(paramType = "query", name = "id", value = "用户ID", required = true, dataType = "String")

     })

     public Message getId( @RequestParam("id") String id) {

         User user = userService.getUser(id);

         return Message.success().add("user", user);

     }

 

     @PostMapping("/save")

     @ApiOperation(value="保存用户", notes="保存用户信息到数据库")

     public Message save(User user) {

         userService.addUser(user);

         return Message.success();

     }

}

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值