반응형
💥 Postman send 버튼 클릭시 발생한 에러 메시지
404 Not Found
❓에러 발생 이유
클라이언트에서 요청한 URL이 서버에 존재하지 않아 나타나는 오류 메시지입니다.
해당 오류 메시지는 Spring(Servlet, Tomcat)이 출력하는 에러 메시지입니다.
예를 들어 서버(Spring, Java)에서는 '/hello'라는 URL로 응답 받기를 설정하였지만, 클라이언트에서는 '/hi'라는 URL로 요청할 경우에 나타나는 에러입니다.
보통은 URL을 잘못 입력하여 나타나는 에러입니다.
혹은 Spring에 해당 URL을 처리하는 Controller이 구현되지 않은 상태입니다.
✅ 해결 방법
✔️ 해결 방법
- Server와 Client의 URL을 통일시켜 줍니다.
Spring의 Controller 예시 코드
@GetMapping("/hello")
public TestUserResponse createUser(@RequestBody TestUserRequest request) {
return new TestUserResponse(request);
}
URL 예시
http://localhost:8080/hi ❌
http://localhost:8080/hello 🟢
응답 결과
참고로 URL을 통일시켜 주면 되기 때문에 아래와 같이 Server(Spring)을 수정하여도 됩니다.
하지만 URL 설계는 프로젝트의 가장 기본이 되는 부분이므로 설계 단계에서 알맞은 URL을 설계하셔야 합니다.
// @GetMapping("/hello") GetMaping URL을 기존 /hello -> /hi로 변경
@GetMapping("/hi")
public TestUserResponse createUser(@RequestBody TestUserRequest request) {
return new TestUserResponse(request);
}
전체 코드
TestController.java
@RestController
public class TestController {
@GetMapping("/hello")
public TestUserResponse createUser(@RequestBody TestUserRequest request) {
return new TestUserResponse(request);
}
@Data
static class TestUserRequest {
private String username;
private int age;
}
@Data
static class TestUserResponse {
private String username;
private int age;
public TestUserResponse(TestUserRequest request) {
this.username = request.getUsername();
this.age = request.getAge();
}
}
}
반응형
댓글