package com.example.exception.dto;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class User {
@NotEmpty
@Size(min = 1, max = 10)
private String name;
@Min(1)
@NotNull
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package com.example.exception.controller;
import com.example.exception.dto.User;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
@RestController
@RequestMapping("/api")
public class RestApiController {
@GetMapping("/user")
public String get(@RequestParam(required = false) String name, @RequestParam(required = false) Integer age) {
User user = new User();
user.setName(name);
user.setAge(age);
int a = 10 + age;
return user.toString();
}
@PostMapping("/user")
public User post(@Valid @RequestBody User user) {
return user;
}
}
package com.example.exception.advice;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class GlobalControllerAdvice {
// value = 내가 잡을 예외
@ExceptionHandler(value = Exception.class)
public ResponseEntity exception(Exception e) {
System.out.println("------------------------------");
System.out.println(e.getLocalizedMessage());
System.out.println("------------------------------");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("");
}
}
package com.example.exception.advice;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
// (basePackages = "com. ...")로 예외처리할 패키지 지정 가능
@RestControllerAdvice()
public class GlobalControllerAdvice {
// value = 내가 잡을 예외
@ExceptionHandler(value = Exception.class)
public ResponseEntity exception(Exception e) {
System.out.println(e.getClass());
System.out.println("------------------------------");
System.out.println(e.getLocalizedMessage());
System.out.println("------------------------------");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("");
}
// 특정 메소드의 예외 처리
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public ResponseEntity methodArgumentNotValidException(MethodArgumentNotValidException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}
}
package com.example.exception.controller;
import com.example.exception.dto.User;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
@RestController
@RequestMapping("/api")
public class RestApiController {
@GetMapping("/user")
public String get(@RequestParam(required = false) String name, @RequestParam(required = false) Integer age) {
User user = new User();
user.setName(name);
user.setAge(age);
int a = 10 + age;
return user.toString();
}
@PostMapping("/user")
public User post(@Valid @RequestBody User user) {
return user;
}
// 특정 메소드의 예외 처리
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public ResponseEntity methodArgumentNotValidException(MethodArgumentNotValidException e) {
System.out.println("rest api conroller");
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}
}