接口生成工具swagger初识

首先在pom.xml文件加入依赖的包:


    io.springfox
    springfox-swagger2
    2.4.0


    io.springfox
    springfox-swagger-ui
    2.4.0

实体类:

package com.example.demo3.model;

import io.swagger.annotations.ApiModel;
import lombok.Data;

import javax.validation.constraints.NotEmpty;

@Data
@ApiModel("用户")
public class User3 {
    private int id;

    @NotEmpty(message = "用户名不能为空")
    private String username;

    @NotEmpty(message = "密码不能为空 ")
    private String password;


}

控制器:

package com.example.demo3.controller;

import com.example.demo3.model.User3;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(value="userInterface",tags={"用户的一些操作接口"})
@RestController
public class UserCotroller {

    @ApiOperation("用户注册-添加用户")
    @PostMapping(value="/api/{username}/{password}/user")
    public Map register(
            @ApiParam(value = "用户注册时需要填写的用户名",required = true)
            @PathVariable(value="username")String username,
            @ApiParam(value = "用户设置的登陆密码", required = true)
            @PathVariable(value="password")String password){
        User3 user = new User3();
        user.setUsername(username);
        user.setPassword(password);
        Map map = new HashMap();
        if(user.getUsername()!=null){
            map.put("code",200);
            map.put("info","注册成功");
        }
        return map;
    }

    @ApiOperation("获取用户信息")
    @GetMapping(value="/api/{username}/{password}/user")
    public Map login(@ApiParam(value="登陆时需填写的用户名",required = true)
                      @PathVariable(value="username")String username,
                      @ApiParam(value="登陆时的密码",required = true)
                      @PathVariable(value="password")String password){
        Map map = new HashMap<>();
        if(username.equals("zs")&&password.equals("123456")){
            map.put("code",200);
            map.put("info","登陆成功");
        }else{
            map.put("code",401);
            map.put("info","用户名或密码错误");
        }
        return map;
    }

    @ApiOperation("删除用户操作")
    @DeleteMapping(value="/api/{id}/user")
    public Map deletUser(@ApiParam(value="根据id做删除操作",required = true)
                          @PathVariable(value="id")int id){
        Map map = new HashMap<>();
        if(id!=0){
            map.put("code",200);
            map.put("info","删除成功");
        }
        return map;
    }

    @ApiOperation("修改用户信息")
    @PatchMapping(value="/api/{id}/{username}/user")
    public Map upadateUser(@ApiParam(value="参照依据id",required = true)
                            @PathVariable(value = "id")int id,
                            @ApiParam(value="修改用户名",required = true)
                            @PathVariable(value = "username")String username){
        Map map = new HashMap<>();
        User3 user=new User3();
        user.setUsername(username);
        if(username.equals(user.getUsername())){
            map.put("code",200);
            map.put("info","修改成功");
        }
        return map;
    }


}

测试访问地址:http://localhost:8080/swagger-ui.html#/

效果图:

接口生成工具swagger初识_第1张图片

你可能感兴趣的:(技术)