인프런 커뮤니티 질문&답변

정민교님의 프로필 이미지

작성한 질문수

스프링 MVC 2편 - 백엔드 웹 개발 활용 기술

스프링 메시지 소스 사용

not found 에러

해결된 질문

24.06.07 00:46 작성

·

342

·

수정됨

0

@Controller
@RequestMapping("/message/items")
@RequiredArgsConstructor
public class MessageItemController {

    private final ItemRepository itemRepository;

    @GetMapping
    public String items(Model model) {
        System.out.println("MessageItemController.items");
        List<Item> items = itemRepository.findAll();
        model.addAttribute("items", items);
        return "message/items";
    }

    @GetMapping("/{itemId}")
    public String item(@PathVariable long itemId, Model model) {
        Item item = itemRepository.findById(itemId);
        model.addAttribute("item", item);
        return "message/item";
    }

...
}

http://localhost:8080/message/items get 요청을 보내면 not found에러가 발생하는데 이유를 찾을수가 없네요.

 

resources static 경로에 있는 index.html은 잘 찾아옵니다.

http://localhost:8080 을 입력하면 브라우저에 페이지가 잘 나와요.

templates 경로도 올바르게 되어있는 것 같은데 이유를 모르겠습니다

 

2024-06-07T00:51:34.677+09:00 DEBUG 20684 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : GET "/message/items", parameters={} 
2024-06-07T00:51:34.724+09:00 DEBUG 20684 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found 
2024-06-07T00:51:34.735+09:00 DEBUG 20684 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.servlet.resource.NoResourceFoundException: No static resource message/items.] 
2024-06-07T00:51:34.736+09:00 DEBUG 20684 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND


debug 로그가 위와 같이 나옵니다.
thymeleaf 의존성을 추가는 했는데 templates 경로에서 찾지 않는 것 같아서요. 맞나요?
의존성 추가는 되어있습니다.

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

 

답변 1

0

OMG님의 프로필 이미지

2024. 06. 07. 07:15

안녕하세요. 정민교님, 공식 서포터즈 OMG입니다.

 

아래 로깅이 출력되는걸로 보아 application.properties 설정파일에 별도 설정을 추가하신 것 같습니다.

image

그리고 로깅 메시지에 items. 뒤에 html이 없는것도 설정파일에 별도 적용된 내용으로 인한 것이지 않나 예상합니다.

image

application.properties 전체 코드를 복사하여 올려주시겠어요?

그리고 타임리프 의존성 추가되어있는지도 확인해주세요^^image

감사합니다.

정민교님의 프로필 이미지
정민교
질문자

2024. 06. 07. 15:39

아 이 부분은 해결했습니다. 다 제대로 되어있었습니다.

프로젝트 메인 메소드를 다른 걸 실행해서 그런 것 같아요. 프로젝트를 spring initailizr로 새로 만들고 그냥 src랑 test 폴더를 복붙하다보니까 main 메소드가 있는 클래스가 두 개가 되었는데, 잘못된 걸 실행했습니다... 이런 멍청한 일로 2시간을 날렸읍니다....