详细配置swagger2

分三步
第一步:
创建一个公共的子模块,在要用到swegger的地方,在子模块的pom文件中引入相关的依赖

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.7.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.7.0</version>
</dependency>

第二步:
创建一个swegger的配置类

package com.example.config;

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;

/**
 * @author zwj
 * @date 2021/4/19 16:00
 * @description
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                // 指定构建api文档的详细信息的方法:apiInfo()
                .apiInfo(apiInfo())
                .select()
                // 指定要生成api接口的包路径
                .apis(RequestHandlerSelectors.basePackage("com.example.control"))
                //使用了 @ApiOperation 注解的方法生成api接口文档
                //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                //可以根据url路径设置哪些请求加入文档,忽略哪些请求
                .build();
    }

    /**
     * 设置api文档的详细信息
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("更多请关注http://www.baidu.com")
                .termsOfServiceUrl("http://www.baidu.com")
                .contact("sunf")
                .version("1.0")
                .build();
    }
}

第三步,在要用的swegger的控制层地方引入依赖

package com.example.control;
import com.example.bean.Userbean;
import com.example.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author zwj
 */
@RequestMapping("/test")
@RestController
@Api(value = "用户信息接口")
public class TestController {

    /**
     * 将Service注入Web层
     */
    @Resource
    UserService userService;


    @GetMapping("/list")
    @ApiOperation("/获取用户列表")
    @ApiImplicitParam(name = "id", value = "id", required = false, paramType = "query", dataType = "String")
    public List<Userbean> get(Integer id, String name, String password) {
        List<Userbean> userbeanList = userService.loginIn(id, name, password);
        return userbeanList;
    }

service层

package com.example.service;
import com.example.bean.Userbean;
import java.util.List;
import java.util.Map;

/**
 * @author zwj
 */
public interface UserService {

    List<Userbean> loginIn(Integer id, String name, String password);

}

impl

package com.example.impl;
import com.example.bean.Userbean;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zwj
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    /**
     * 将DAO注入Service层
     */
    @Resource
    private UserMapper userMapper;

    @Override
    public List<Userbean> loginIn(Integer id, String name, String password) {
        List<Userbean> info = userMapper.getInfo(id, name, password);
        return info;
    }

mapper

package com.example.mapper;
import com.example.bean.Userbean;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;

public interface UserMapper {

    List<Userbean> getInfo(Integer id, String name, String password);
    }

xml

   <select id="getInfo" parameterType="list" resultType="com.example.bean.Userbean">
        SELECT *
        FROM user
    </select>

swagger:的默认访问地址
http://localhost:8080/swagger-ui.html
效果就是这样的
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

故事的小黄花-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值