SpringBoot集成swagger接口文档

SpringBoot+Swagger

1. 先创建一个maven项目 在pom.xml中引入SpringBoot和swagger的依赖

     
<!-- parent 整合第三方常用框架依赖信息(各种依赖信息) -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
  </parent>
   <!-- 实现原理:Maven依赖继承管理   -->
   <!--相当于 把第三方常用的Maven依赖信息 在parent项目中已经封装好了 使用springboot提供依赖信息关联整合jar包 -->
   <dependencies>
   <dependency>  
         <groupId>org.springframework.boot</groupId>  
         <artifactId>spring-boot-starter-web</artifactId>  
     </dependency>
    <dependency>  
         <groupId>org.springframework.boot</groupId>  
         <artifactId>spring-boot-starter-thymeleaf</artifactId>  
     </dependency>
     <!-- swagger ui -->
        <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>
   </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  
</project>

2. 然后再创建一个启动类 注意主方法上的@EnableSwagger2 注解是启用swagger

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@SpringBootApplication
@MapperScan("com.wys.mapper")//将项目中对应的mapper类的路径加进来就可以了
public class Applicationcontroller {
   public static void main(String[] args) { 
	 SpringApplication.run(Applicationcontroller.class, args);
   }
}

**3.**然后创建对应的类

  实体类加注解@APIModel

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(value="User用户实体",reference="参考")
public class User{
   @ApiModelProperty(value="唯一用户id",dataType="String",required=true)
   private String id;
   @ApiModelProperty(value="用户名称",dataType="String")
   private String name;
   @ApiModelProperty(value="用户密码",dataType="String")
   private String password;
   @ApiModelProperty(value="手机号码",dataType="String")
   private String phone;
   @ApiModelProperty(value="创建时间",dataType="String")
   private String createtime;
   @ApiModelProperty(value="预留字段1",dataType="String")
   private String obligate1;
   @ApiModelProperty(value="预留字段2",dataType="String")
   private String obligate2;
     ....省略get set方法

**4.**创建controller


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
@Api(value="Admincontr")
@RestController
@RequestMapping("/indexcontr")
public class Admincontr {
@ApiOperation(value="后台登录",notes="返回map,success:成功,message:失败")
	 @RequestMapping(value="/login",method=RequestMethod.POST)
	 @ApiImplicitParams(
				{@ApiImplicitParam(paramType = "query", dataType = "String", name = "nickName", value = "用户名", required = true),
				 @ApiImplicitParam(paramType = "query", dataType = "String", name = "passWord", value = "密码", required = true)
					})
	   public Map<String,Object> login(HttpSession session,User user){
			Map<String,Object> map=new HashMap<String, Object>();
			User list=userImpl.login(user);
			if(list!=null){
				session.setAttribute("users", list);
				map.put("success", list);
			}else {
				map.put("message", "账户名或密码错误");
			}
			return map;
	   }
}

5 最关键的一步 要找对swagger-UI的展示页面 我当时不知道。。。。。

       启动项目   然后去浏览器地址 输入swagger展示页面 (页面是固定的)↓↓↓
       localhost:8080/swagger-ui.html
       
       8080是你的tomcat端口号
       然后就可以看到你写的swagger了   

注:我只是加的部分注解 有些注解需要自己去百度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值