728x90

pom.xml

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
<dependency>

메인 클래스에 @EnableOpenApi 추가

@EnableOpenApi
@SpringBootApplication
public class DemoApplication {

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

}

샘플 Controller 작성 해보기 

@Api(tags="사용자관리")
@RestController
public class UserController {

    @ApiOperation("사용자생성")
    @PostMapping("/users")
    public User create(@RequestBody @Valid User user) {
        return user;
    }

    @ApiOperation("사용자상세")
    @GetMapping("/users/{id}")
    public User findById(@PathVariable Long id) {
        return new User("bbb", 21, "인천", "aaa@bbb.com");
    }

    @ApiOperation("사용자목록")
    @GetMapping("/users")
    public List<User> list(@ApiParam("조회페이지") @RequestParam int pageIndex,
                           @ApiParam("페이당건수") @RequestParam int pageSize) {
        List<User> result = new ArrayList<>();
        result.add(new User("aaa", 50, "서울", "aaa@ccc.com"));
        result.add(new User("bbb", 21, "인천", "aaa@ddd.com"));
        return result;
    }

    @ApiIgnore
    @DeleteMapping("/users/{id}")
    public String deleteById(@PathVariable Long id) {
        return "delete user : " + id;
    }

}

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("사용자기본정보")
public class User {

    @ApiModelProperty("이름")
    @Size(max = 20)
    private String name;
    @ApiModelProperty("나이")
    @Max(150)
    @Min(1)
    private Integer age;
    @NotNull
    private String address;
    @Pattern(regexp = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$")
    private String email;

}

http://localhost:8080/swagger-ui/index.html 접속후 확인!

끝!

+ Recent posts