Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test : Swagger 설정 및 API 문서화 #51 #52

Merged
merged 1 commit into from
Jun 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion TogeDutch/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'org.springframework.boot:spring-boot-starter-mail'

// 추가 끝
// swagger
//https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui
implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
implementation group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'

compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.proj.togedutch.config;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
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;

@Configuration // 스프링 실행 시 설정파일을 읽어들이기 위한 Annotation
@EnableSwagger2 // Swagger2를 사용한다는 Annotation
public class SwaggerConfig {
//swagger 설정
public Docket getDocket(String groupName, Predicate<String> predicate) {
return new Docket(DocumentationType.SWAGGER_2)
.groupName(groupName)
.select()
.apis(RequestHandlerSelectors.basePackage("com.proj.togedutch"))
.paths(predicate)
.apis(RequestHandlerSelectors.any())
.build();
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("가치더치 Spring Boot REST API")
.version("1.0.0")
.description("JPA 버전 SWAGGER API 입니다.")
.build();
}

@Bean
public Docket postAPI() {
return getDocket("공고", Predicates.or(
PathSelectors.regex("/post.*")));
}

@Bean
public Docket matchingAPI() {
return getDocket("매칭", Predicates.or(
PathSelectors.regex("/matching.*")));
}

@Bean
public Docket likePostAPI() {
return getDocket("관심목록", Predicates.or(
PathSelectors.ant("/user/{userIdx}/likePost.*")));
}

@Bean
public Docket chatRoomAPI() {
return getDocket("채팅방", Predicates.or(
PathSelectors.regex("/chatRoom.*")));
}

@Bean
public Docket chatAPI() {
return getDocket("채팅", Predicates.or(
PathSelectors.ant("/chatRoom/{chatRoom_id}.*")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import com.proj.togedutch.service.AWSS3Service;
import com.proj.togedutch.service.ChatRoomService;
import com.proj.togedutch.service.PostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -21,6 +25,7 @@
@RestController
@RequiredArgsConstructor
@RequestMapping("/post")
@Api(tags = {"공고 API"}) // Swagger 최상단 Controller 명칭
public class PostController {
final Logger logger = LoggerFactory.getLogger(this.getClass());
private final PostService postService;
Expand All @@ -31,6 +36,7 @@ public class PostController {

// 공고 전체 조회
@GetMapping("")
@ApiOperation(value = "공고 전체 조회", notes = "모든 공고를 조회합니다.")
public BaseResponse<List<PostResDto>> getAllPosts() {
try{
List<PostResDto> getPostsRes = postService.findAll();
Expand All @@ -42,6 +48,12 @@ public BaseResponse<List<PostResDto>> getAllPosts() {

// 공고 생성 (채팅방도 생성)
@PostMapping("")
@ApiOperation(value = "공고 생성", notes = "공고와 채팅방을 생성합니다.")
@ApiImplicitParams({
@ApiImplicitParam(name = "user", value = "공고 생성자의 user_id"),
@ApiImplicitParam(name = "post", value = "공고 생성시 작성한 내용"),
@ApiImplicitParam(name = "file", value = "공고 생성시 첨부한 이미지")
})
public BaseResponse<PostResDto> createPost(@RequestParam int user, @RequestPart PostReqDto post, @RequestPart(value="file", required = false) MultipartFile file) throws IOException {
String fileUrl = null;
int createPostIdx, insertIdx;
Expand Down
18 changes: 18 additions & 0 deletions TogeDutch/src/main/java/com/proj/togedutch/dto/PostResDto.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.proj.togedutch.dto;

import com.proj.togedutch.domain.Post;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -10,22 +11,39 @@
@Getter
@NoArgsConstructor
public class PostResDto {
@ApiModelProperty(example = "공고 생성시 부여된 id")
private int post_id;
@ApiModelProperty(example = "제목")
private String title;
@ApiModelProperty(example = "첨부한 URL")
private String url;
@ApiModelProperty(example = "배달팁")
private int delivery_tips;
@ApiModelProperty(example = "최소 주문 금액")
private int minimum;
@ApiModelProperty(example = "주문 시간")
private String order_time;
@ApiModelProperty(example = "모집 인원")
private int num_of_recruits;
@ApiModelProperty(example = "모집된 인원")
private int recruited_num;
@ApiModelProperty(example = "공고 상태")
private String status;
@ApiModelProperty(example = "최초 작성 시간")
private Timestamp created_at;
@ApiModelProperty(example = "마지막으로 수정한 시간")
private Timestamp updated_at;
@ApiModelProperty(example = "공고 생성자의 id")
private int user_id;
@ApiModelProperty(example = "첨부한 이미지 URL")
private String image;
@ApiModelProperty(example = "위도")
private Double latitude;
@ApiModelProperty(example = "경도")
private Double longitude;
@ApiModelProperty(example = "채팅방 생성시 부여된 id")
private Integer chatRoom_id;
@ApiModelProperty(example = "카테고리")
private String category;

// Entity to Dto
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public UserResDto(UserRepository.UserInfo user) {
this.password = user.getPassword();
this.phone = user.getPhone();
this.image = user.getImage() == null ? null : imageConvert(user.getImage());
this.image = user.getImage();
this.status = user.getStatus();
this.created_at = user.getCreated_at();
this.updated_at = user.getUpdated_at();
Expand Down