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

과제 완료 #32

Open
wants to merge 4 commits into
base: 24HwangChabin-mvc
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
sourceCompatibility = '8'

repositories {
mavenCentral()
Expand Down
14 changes: 13 additions & 1 deletion docs/spring-mvc.md
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
# 요기다 정리
# 요기다 정리
` @ResponseStatus `:응답 상태 지정가능
```
@RequestParam(required = false) String name,
@RequestHeader(value = "HEADER", required = false) String header
```
required를 통해, 값이 필수인지를 정할 수 있음.
값이 필수가 아닐때, 해당 값이 안들어온경우 null을 반환함.
분기를 나눠서, 케이스를 나눠 처리 가능,`HttpMethodController`처럼.

MediaType.APPLICATION_JSON_VALUE 와
MediaType.TEXT_HTML_VALUE 를 통해
반환 형식별로 지정해서 사용
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> handle() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
public class MethodArgumentController {

@GetMapping("/users")
public ResponseEntity<List<User>> requestParam(String userName) {
public ResponseEntity<List<User>> requestParam(@RequestParam(required = false) String name) {
String userName=name;
List<User> users = Arrays.asList(
new User(userName, "email"),
new User(userName, "email")
Expand All @@ -22,7 +23,7 @@ public ResponseEntity<List<User>> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> string() {
return ResponseEntity.ok().body("message");
}
@GetMapping("/users")
public ResponseEntity<User> responseBodyForUser() {
User user = new User("name","email");
return ResponseEntity.ok().body(user);
}
@GetMapping("/users/{id}")
public ResponseEntity<User> responseEntity(@PathVariable Long id) {
User user = new User(id,"name","email");
return ResponseEntity.ok().body(user);
}
@GetMapping("/members")
public ResponseEntity<Object> responseEntityFor400() {
return ResponseEntity.badRequest().build();
}
@GetMapping("thymeleaf")
public ResponseEntity<String> thymeleaf() {
return ResponseEntity.ok().body("Hello");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<User>> showUser() {
List<User> users = Arrays.asList(
new User("이름", "email"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<User>> showUser() {
List<User> users = Arrays.asList(
new User("이름", "email"),
new User("이름", "email")
new User("이름", "[email protected]"),
new User("이름", "[email protected]")
);
return ResponseEntity.ok().body(users);
}

public String userPage() {
return "user page";
@GetMapping(value = "/users", produces = MediaType.TEXT_HTML_VALUE)
public ResponseEntity<String> userPage() {
return ResponseEntity.ok("user page");
}
}
Original file line number Diff line number Diff line change
@@ -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<String> messageForParam(
@RequestParam(required = false) String name,
@RequestHeader(value = "HEADER", required = false) String header) {

public ResponseEntity<String> message() {
return ResponseEntity.ok().body("message");
}
// HEADER 헤더가 "hi"인 경우 다른 응답
if ("hi".equals(header)) {
return ResponseEntity.ok().body("hi");
}

public ResponseEntity<String> messageForParam() {
return ResponseEntity.ok().body("hello");
}

public ResponseEntity<String> messageForHeader() {
return ResponseEntity.ok().body("hi");
}
}
// HEADER 헤더가 없을 경우 또는 "hi"가 아닐 경우 기본 응답
if (name != null) {
return ResponseEntity.ok().body("hello");
} else {
return ResponseEntity.ok().body("message");
}
}}
Original file line number Diff line number Diff line change
@@ -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<User> pathVariable(Long id) {
User user = new User(id, "이름", "email");
@GetMapping(value = "/users/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> pathVariable(@PathVariable Long id) {
User user = new User(id, "이름", "[email protected]");
return ResponseEntity.ok().body(user);
}

public ResponseEntity<String> pattern() {
@GetMapping("patterns/{value}")
public ResponseEntity<String> pattern(@PathVariable String value) {
return ResponseEntity.ok().body("pattern");
}

@GetMapping("patterns/**")//대체이부분 어떻게 해결..?
public ResponseEntity<String> patternStars() {
return ResponseEntity.ok().body("pattern-multi");
}
Expand Down