swagger初步架构

1.导入jar包`

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

2.配置congfig,添加到容器中

package com.tedu.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;

import java.util.ArrayList;
//固定格式
@Configuration//配置类
@EnableSwagger2//添加到
public class SwaggerConfig {
    @Bean//将Docket添加到容器中
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("cuijianxin");
    }
    @Bean
    public Docket docket() {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //一个Docket对应一个组名!!开发中可能要处理多个项目,所以需要组!
                .groupName("崔建新")
                .select()
                //定义在哪个包下面扫描,只搜扫描Controller类里(接受请求的类)!!!!!
                .apis(RequestHandlerSelectors.basePackage("com.tedu.controller"))
                //basePackage下全扫描,none()全不扫描
                .paths(PathSelectors.any())
                .build();

    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //直接这两个就可以,其他的都没什么用
                //标题!!!!
                .title("婚纱摄影后台接口管理系统")
                //描述信息!!!!
                .description("我们的小组")
                .build();
    }
}

3.Controller

package com.tedu.controller;

import com.tedu.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@Api(description = "管理员工")
@RestController
public class HelloController {

    @ApiOperation("user测试类")//给类注解
    @GetMapping("/hello")
    public User hello(User user){
    return user;
    }

    @ApiOperation("user测试类")//给类注解
    @PostMapping("/hello")
    //加@Requestbody,实体类在swagger中直接有
    public User hello1(@RequestBody User user){
        return user;
    }

}

4. 标题实体类

package com.tedu.pojo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("用户实体类")
public class User {
    @ApiModelProperty("用户名")
    public String username;
    @ApiModelProperty("密码")
    public String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

最后一步
访问 http://localhost:8080/swagger-ui.html(项目的端口号)

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值