swagger用法教程

swagger用法

1、导入maven依赖:

<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、swagger配置文件,与启动文件统一目录级别:

package com.xxx.edge;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
 * decription  http://localhost:15080/swagger-ui.html
 * @author wangjingjing
 * @date 2020/8/7 13:52
 */
@Configuration
@EnableSwagger2   //这个要加上!!!
public class Swagger2 {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xxx.edge.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("边缘服务接口文档")
                .description("代码地址 http://xxx.xxx.xx.xx/wangjingjing/edge-service.git")
                .termsOfServiceUrl("http://xxx.xxx.xx.xx/wangjingjing/edge-service.git")
                .version("1.0")
                .build();
    }
}

3、controller编写:

package com.xxx.edge.controller;

import com.xxx.edge.vo.ServiceMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Api(tags = "通信接口")  //功能:描述controller类
@RequestMapping("/api")
@Controller
public class DeviceRequestController {


    //普通参数例子
    //描述一个方法或者一个API接口。value = "" // 描述方法。notes = "" // 描述方法详细信息
    @ApiOperation(value = "功能描述", notes = "功能描述。。。。。")
    //描述方法或接口参数,name:方法或接口的形参, 注意要与方法的参数名称相同。 value:对参数的描述
    //paramType = ""  参数传递方式,此属性的可选值 ["header", "query", "path", "body", "form"]
           // header,使用@RequestHeader获取的参数
           //query,使用@RequestParam获取的参数,常用于GET请求
           //path,使用@PathVariable获取的参数
           //body,使用@RequestBody获取的参数,常用于POST请求,对象参数
    //dataType = "" 参数类型,例如 string, int, ArrayList, POJO类
    @ApiImplicitParam(name = "param", value = "参数描述", required = true, dataType = "String") 
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    @ResponseBody
    public String test(@RequestParam("param") String param) {
        return "ok ok";
    }

    //实体例子
    @ApiOperation(value = "更新用户详细信息", notes = "根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
    //@ApiImplicitParams汇集多个参数
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "serviceMessage", value = "用户详细实体v", required = true, dataType = "ServiceMessage")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String putUser(@PathVariable Long id, @RequestBody ServiceMessage serviceMessage) {

        return "success !";
    }
}

controller请求如果写成@GetMapping这种写法会把所有的方法请求方式都展列出来(get,put,post,delete)

 

 

各种请求例子写法:

@RestController
@RequestMapping(value="/users")     // 通过这里配置使下面的映射都在/users下,可去除
public class UserController {

    static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());

    @ApiOperation(value="获取用户列表", notes="")
    @RequestMapping(value={""}, method=RequestMethod.GET)
    public List<User> getUserList() {
        List<User> r = new ArrayList<User>(users.values());
        return r;
    }

    @ApiOperation(value="创建用户", notes="根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @RequestMapping(value="", method=RequestMethod.POST)
    public String postUser(@RequestBody User user) {
        users.put(user.getId(), user);
        return "success";
    }

    @ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
    @RequestMapping(value="/{id}", method=RequestMethod.GET)
    public User getUser(@PathVariable Long id) {
        return users.get(id);
    }

    @ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    })
    @RequestMapping(value="/{id}", method=RequestMethod.PUT)
    public String putUser(@PathVariable Long id, @RequestBody User user) {
        User u = users.get(id);
        u.setName(user.getName());
        u.setAge(user.getAge());
        users.put(id, u);
        return "success";
    }

    @ApiOperation(value="删除用户", notes="根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
    @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
    public String deleteUser(@PathVariable Long id) {
        users.remove(id);
        return "success";
    }

4、 如果想忽略展示某些接口,在类或者方法上加注解:

a.在类上加注解忽略

@ApiIgnore   //忽略展示接口
@Controller
public class IndexController {
}

b.在方法上加注解忽略

//实体例子
@ApiIgnore //不展示该接口
@ApiOperation(value = "更新用户详细信息", notes = "根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
        @ApiImplicitParam(name = "serviceMessage", value = "用户详细实体v", required = true, dataType = "ServiceMessage")
})
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public String putUser(@PathVariable Long id, @RequestBody ServiceMessage serviceMessage) {

    return "success !";
}

5、访问地址:http://localhost:15080/swagger-ui.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值