Skip to content

Commit

Permalink
feat: Swagger And Exception Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
redcarrot1 committed Dec 28, 2023
1 parent d126266 commit bcd9357
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/main/java/solution/gdsc/PathPal/ImageNotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package solution.gdsc.PathPal;

import solution.gdsc.PathPal.global.error.ErrorCode;
import solution.gdsc.PathPal.global.error.baseExcaption.NotFoundException;

public class ImageNotFoundException extends NotFoundException {
public ImageNotFoundException() {
super(ErrorCode.IMAGE_NOT_FOUND);
}
}
17 changes: 12 additions & 5 deletions src/main/java/solution/gdsc/PathPal/MainController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package solution.gdsc.PathPal;

import io.swagger.v3.oas.annotations.Operation;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
Expand All @@ -9,33 +10,39 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

@RestController
public class MainController {

//@Value("${image.path}")
private String path = "/home/hsk4991149/static/image/";
//private String path = "/Users/hongseungtaeg/Desktop/project/GDSC-PathPal/PathPal/src/main/resources/static/";

@GetMapping
@Operation(summary = "index page", description = "index page Test")
public String index() {
return "hello world!";
}

@PostMapping("/test")
@Operation(summary = "Post request body(json)", description = "request body(json) Test")
public TestResponse test(@RequestBody TestRequest testRequest) {
return TestResponse.from(testRequest.getName());
}

@GetMapping("/image/{imageId}")
@Operation(summary = "이미지 조회", description = "WebSocket으로 저장된 이미지를 조회합니다.(Content-Type: image/jpeg)")
public ResponseEntity<Resource> viewImg(@PathVariable(name = "imageId") Integer imageId) throws IOException {
final String fileFullPath = path + imageId + ".jpeg";
System.out.println(fileFullPath);
Path path = new File(fileFullPath).toPath();
File file = new File(fileFullPath);

FileSystemResource resource = new FileSystemResource(path);
if (!file.exists()) {
throw new ImageNotFoundException();
}

FileSystemResource resource = new FileSystemResource(file);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(Files.probeContentType(path)))
.contentType(MediaType.parseMediaType(Files.probeContentType(file.toPath())))
.body(resource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

@OpenAPIDefinition(
info = @Info(
title = "Greenmate API 명세서",
description = "alpha-test를 위한 Greenmate API 명세서입니다.",
title = "PathPal API 명세서",
description = "GDSC Solution Challenge - PathPal API 명세서입니다.",
version = "0.1"
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class WebSocketHandler extends BinaryWebSocketHandler {

//@Value("${image.path:/home/hsk4991149/static/image/}")
private String path = "/home/hsk4991149/static/image/";
//private String path = "/Users/hongseungtaeg/Desktop/project/GDSC-PathPal/PathPal/src/main/resources/static/";

private final Set<WebSocketSession> sessions = ConcurrentHashMap.newKeySet();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public enum ErrorCode {
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "C005", "서버 내부에서 에러가 발생하였습니다."),
BAD_REQUEST(HttpStatus.BAD_REQUEST, "C006", "Bad Request"),

// image
IMAGE_NOT_FOUND(HttpStatus.BAD_REQUEST, "I001", "이미지를 찾을 수 없습니다."),

;

private final HttpStatus status;
Expand Down

0 comments on commit bcd9357

Please sign in to comment.