Swagger使用方法

本文介绍了Swagger的使用方法,包括在pom中引入依赖、在启动类加入@EnableSwagger2注解、在controller层的使用方法,还给出了Api访问地址及示例,访问方式为url:port/微服务名称/swagger-ui.html。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Swagger使用方法

1. pom引入依赖

        <!--Swagger-->
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>27.1-jre</version>
        </dependency>

2. 启动类加入@EnableSwagger2注解

package com.sgcc.dlsc.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@MapperScan("com.sgcc.dlsc.config.dao")
@EnableFeignClients
@EnableSwagger2
public class ConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }

}

3. 在controller层的使用方法

package com.sgcc.dlsc.config.controller;

import com.sgcc.comm.model.PageQuery;
import com.sgcc.comm.util.ResultUtil;
import com.sgcc.dlsc.config.model.po.DemoCourse;
import com.sgcc.dlsc.config.service.DemoCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @创建人: 杨文培
 * @创建时间: 2019/5/18 19:22
 * @描述:
 */
@RequestMapping("/course")
@RestController
@Slf4j
@Api("课程管理")
public class DemoCourseController {
    @Autowired
    private DemoCourseService demoCourseService;

    /**
     * @param
     * @return
     */
    @ApiOperation("分页查询课程")
    @PostMapping("/queryPage")
    public ResultUtil queryPage(@RequestBody PageQuery<DemoCourse> pageQuery){
        log.info("分页查询测试");
        return demoCourseService.queryPage(pageQuery);
    }
}

4. Api访问地址

  1. 访问方式:url:port/微服务名称/swagger-ui.html
  2. 访问示例:http://localhost:9000/px-trade-config/swagger-ui.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值