diff --git a/build.gradle b/build.gradle index 4964f025..a39609d4 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { group = 'com.example' version = '0.0.1-SNAPSHOT' -sourceCompatibility = '11' +sourceCompatibility = '8' repositories { mavenCentral() diff --git a/docs/spring-mvc.md b/docs/spring-mvc.md index 94ebd6a9..88f1279e 100644 --- a/docs/spring-mvc.md +++ b/docs/spring-mvc.md @@ -1 +1,13 @@ -# 요기다 정리 \ No newline at end of file +# 요기다 정리 +` @ResponseStatus `:응답 상태 지정가능 +``` +@RequestParam(required = false) String name, +@RequestHeader(value = "HEADER", required = false) String header +``` +required를 통해, 값이 필수인지를 정할 수 있음. +값이 필수가 아닐때, 해당 값이 안들어온경우 null을 반환함. +분기를 나눠서, 케이스를 나눠 처리 가능,`HttpMethodController`처럼. + +MediaType.APPLICATION_JSON_VALUE 와 +MediaType.TEXT_HTML_VALUE 를 통해 +반환 형식별로 지정해서 사용 \ No newline at end of file diff --git a/src/main/java/nextstep/helloworld/mvc/exceptions/ExceptionsController.java b/src/main/java/nextstep/helloworld/mvc/exceptions/ExceptionsController.java index 07cd6fae..7e1b991c 100644 --- a/src/main/java/nextstep/helloworld/mvc/exceptions/ExceptionsController.java +++ b/src/main/java/nextstep/helloworld/mvc/exceptions/ExceptionsController.java @@ -2,24 +2,24 @@ import nextstep.helloworld.mvc.exceptions.exception.CustomException; import nextstep.helloworld.mvc.exceptions.exception.HelloException; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/exceptions") public class ExceptionsController { @GetMapping("/hello") + @ResponseStatus(HttpStatus.BAD_REQUEST) public ResponseEntity exceptionHandler() { - throw new CustomException(); + return ResponseEntity.badRequest().body("CustomException"); } @GetMapping("/hi") + @ResponseStatus(HttpStatus.BAD_REQUEST) public ResponseEntity exceptionHandler2() { - throw new HelloException(); + return ResponseEntity.badRequest().body("HelloException"); } public ResponseEntity handle() { diff --git a/src/main/java/nextstep/helloworld/mvc/handler/MethodArgumentController.java b/src/main/java/nextstep/helloworld/mvc/handler/MethodArgumentController.java index 49fc35b3..c7dbc107 100644 --- a/src/main/java/nextstep/helloworld/mvc/handler/MethodArgumentController.java +++ b/src/main/java/nextstep/helloworld/mvc/handler/MethodArgumentController.java @@ -13,7 +13,8 @@ public class MethodArgumentController { @GetMapping("/users") - public ResponseEntity> requestParam(String userName) { + public ResponseEntity> requestParam(@RequestParam(required = false) String name) { + String userName=name; List users = Arrays.asList( new User(userName, "email"), new User(userName, "email") @@ -22,7 +23,7 @@ public ResponseEntity> requestParam(String userName) { } @PostMapping("/users/body") - public ResponseEntity requestBody(User user) { + public ResponseEntity requestBody(@RequestBody User user) { User newUser = new User(1L, user.getName(), user.getEmail()); return ResponseEntity.created(URI.create("/users/" + newUser.getId())).body(newUser); } diff --git a/src/main/java/nextstep/helloworld/mvc/handler/ReturnValueController.java b/src/main/java/nextstep/helloworld/mvc/handler/ReturnValueController.java index 107eaa53..a3128306 100644 --- a/src/main/java/nextstep/helloworld/mvc/handler/ReturnValueController.java +++ b/src/main/java/nextstep/helloworld/mvc/handler/ReturnValueController.java @@ -3,30 +3,32 @@ import nextstep.helloworld.mvc.domain.User; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; @Controller @RequestMapping("/return-value") public class ReturnValueController { -// public void string() { -// return "message"; -// } -// -// public void responseBodyForUser() { -// return new User("name", "email"); -// } -// -// public void responseEntity(@PathVariable Long id) { -// return ResponseEntity.ok(new User("name", "email")); -// } -// -// public void responseEntityFor400() { -// return ResponseEntity.badRequest().build(); -// } -// -// public void thymeleaf() { -// return "sample"; -// } + @GetMapping("/message") + public ResponseEntity string() { + return ResponseEntity.ok().body("message"); + } + @GetMapping("/users") + public ResponseEntity responseBodyForUser() { + User user = new User("name","email"); + return ResponseEntity.ok().body(user); + } + @GetMapping("/users/{id}") + public ResponseEntity responseEntity(@PathVariable Long id) { + User user = new User(id,"name","email"); + return ResponseEntity.ok().body(user); + } + @GetMapping("/members") + public ResponseEntity responseEntityFor400() { + return ResponseEntity.badRequest().build(); + } + @GetMapping("thymeleaf") + public ResponseEntity thymeleaf() { + return ResponseEntity.ok().body("Hello"); + } } \ No newline at end of file diff --git a/src/main/java/nextstep/helloworld/mvc/mapping/HttpMethodController.java b/src/main/java/nextstep/helloworld/mvc/mapping/HttpMethodController.java index 10b38fa9..42623b9c 100644 --- a/src/main/java/nextstep/helloworld/mvc/mapping/HttpMethodController.java +++ b/src/main/java/nextstep/helloworld/mvc/mapping/HttpMethodController.java @@ -9,13 +9,16 @@ import java.util.List; @RestController +@RequestMapping("/http-method") public class HttpMethodController { + @PostMapping("/users") public ResponseEntity createUser(@RequestBody User user) { Long id = 1L; return ResponseEntity.created(URI.create("/users/" + id)).build(); } + @GetMapping("/users") public ResponseEntity> showUser() { List users = Arrays.asList( new User("이름", "email"), diff --git a/src/main/java/nextstep/helloworld/mvc/mapping/MediaTypeController.java b/src/main/java/nextstep/helloworld/mvc/mapping/MediaTypeController.java index 1125fbdd..8252555b 100644 --- a/src/main/java/nextstep/helloworld/mvc/mapping/MediaTypeController.java +++ b/src/main/java/nextstep/helloworld/mvc/mapping/MediaTypeController.java @@ -13,20 +13,22 @@ @RequestMapping("/media-type") public class MediaTypeController { + @PostMapping("/users") public ResponseEntity createUser(@RequestBody User user) { Long id = 1L; return ResponseEntity.created(URI.create("/users/" + id)).build(); } - + @GetMapping(value = "/users", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity> showUser() { List users = Arrays.asList( - new User("이름", "email"), - new User("이름", "email") + new User("이름", "email1@example.com"), + new User("이름", "email2@example.com") ); return ResponseEntity.ok().body(users); } - public String userPage() { - return "user page"; + @GetMapping(value = "/users", produces = MediaType.TEXT_HTML_VALUE) + public ResponseEntity userPage() { + return ResponseEntity.ok("user page"); } } \ No newline at end of file diff --git a/src/main/java/nextstep/helloworld/mvc/mapping/ParamHeaderController.java b/src/main/java/nextstep/helloworld/mvc/mapping/ParamHeaderController.java index 4a2d03c9..79afbd4c 100644 --- a/src/main/java/nextstep/helloworld/mvc/mapping/ParamHeaderController.java +++ b/src/main/java/nextstep/helloworld/mvc/mapping/ParamHeaderController.java @@ -1,23 +1,25 @@ package nextstep.helloworld.mvc.mapping; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/param-header") public class ParamHeaderController { + @GetMapping("/message") + public ResponseEntity messageForParam( + @RequestParam(required = false) String name, + @RequestHeader(value = "HEADER", required = false) String header) { - public ResponseEntity message() { - return ResponseEntity.ok().body("message"); - } + // HEADER 헤더가 "hi"인 경우 다른 응답 + if ("hi".equals(header)) { + return ResponseEntity.ok().body("hi"); + } - public ResponseEntity messageForParam() { - return ResponseEntity.ok().body("hello"); - } - - public ResponseEntity messageForHeader() { - return ResponseEntity.ok().body("hi"); - } -} \ No newline at end of file + // HEADER 헤더가 없을 경우 또는 "hi"가 아닐 경우 기본 응답 + if (name != null) { + return ResponseEntity.ok().body("hello"); + } else { + return ResponseEntity.ok().body("message"); + } +}} \ No newline at end of file diff --git a/src/main/java/nextstep/helloworld/mvc/mapping/UriPatternController.java b/src/main/java/nextstep/helloworld/mvc/mapping/UriPatternController.java index 5979bed2..91f6bdd3 100644 --- a/src/main/java/nextstep/helloworld/mvc/mapping/UriPatternController.java +++ b/src/main/java/nextstep/helloworld/mvc/mapping/UriPatternController.java @@ -1,23 +1,24 @@ package nextstep.helloworld.mvc.mapping; import nextstep.helloworld.mvc.domain.User; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/uri-pattern") public class UriPatternController { - public ResponseEntity pathVariable(Long id) { - User user = new User(id, "이름", "email"); + @GetMapping(value = "/users/{id}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity pathVariable(@PathVariable Long id) { + User user = new User(id, "이름", "email@example.com"); return ResponseEntity.ok().body(user); } - - public ResponseEntity pattern() { + @GetMapping("patterns/{value}") + public ResponseEntity pattern(@PathVariable String value) { return ResponseEntity.ok().body("pattern"); } - + @GetMapping("patterns/**")//대체이부분 어떻게 해결..? public ResponseEntity patternStars() { return ResponseEntity.ok().body("pattern-multi"); }