SSM 项目中加入Swagger

Swagger


目录

Swagger

一 maven中添加依赖

二 配置swagger

三 spring-mvc中进行自动扫包

四 添加相关注释


个人感觉Swagger比Postman更加的直观而方便的进行调试

下面我们先预览一下最基本的功能

 

可以直观的看到项目中的api,一级各个api的功能以及请求方式,比手搓api文档好多了。。

当然也可以进行各个接口的测试:

进行简单的测试,查看数据是否传输正确,非常方便。

下面我们来看看怎么将swagger整合到ssm中吧

 

一 maven中添加依赖

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

二 配置swagger

简单的配置,目录结构

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(true)          // 是否禁用swagger
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("swagger API文档")
                .description("Dcpnet商品管理系统接口文档")
                .version("1.0")
                .build();
    }
}

三 spring-mvc中进行自动扫包

<context:component-scan base-package="com.dcpnet.swagger"/>

四 添加相关注释

首先再Controller外层添加@Api的注解,然后再内部使用@ApiOperation的注解,在里面进行配置,更多的配置请到官网查看

@RestController
@RequestMapping("/user")
@Api(value = "UserController管理",tags = "UserController管理接口API")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/chaXunById")
    @ApiOperation(value = "通过id查询对象接口",notes = "通过id查询对象接口",httpMethod = "get")
    public User chaXunById(Integer id) {
        return this.userService.chaXunById(id);
    }
    
    /**
     * 通过主键删除单条数据
     *
     * @param id 主键
     * @return Map
     */
    @RequestMapping(value = "/shanChuById",method = RequestMethod.POST)
    @ApiOperation(value = "通过id删除对象接口",notes = "通过id删除对象接口",httpMethod = "post")
    public Map<String,Object> shanChuById(@Param("id")Integer id){
        return this.userService.shanChuById(id);
    }

}

 

我发布过一个SSM模板项目(脚手架项目),里面有配置好的swagger,以及editor.md,可以直接使用。谢谢

资源链接 脚手架项目

脚手架项目博客

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值