개발/HTML

Error resolving template, template might not exist or might not be accessible by any of the configured Template Resolvers

aihtnyc_h 2024. 11. 1. 21:06
728x90
반응형
SMALL

### 에러 노트: Thymeleaf 템플릿 처리 오류

#### 문제 설명

- **에러 메시지**:
    

    `[THYMELEAF][http-nio-9090-exec-1] Exception processing template 
    "alimTalk/countAlimCenter": Error resolving template [alimTalk/countAlimCenter], 
    template might not exist or might not be accessible by any of the configured 
    Template Resolvers`



    
- **원인**: HTML을 반환하지 않고 결과만 받아야 하는 상황에서 발생한 에러입니다. `Thymeleaf` 템플릿 처리 과정에서 지정한 템플릿을 찾을 수 없거나 접근할 수 없다는 의미입니다.

#### 발생 상황

HTML 대신 JSON만 반환하는 컨트롤러에서 `Thymeleaf` 템플릿을 처리하려고 할 때 발생합니다. 이러한 경우, 불필요한 템플릿 처리를 시도하게 되어 오류가 발생합니다.

#### 해결 방안

- JSON 응답을 반환하려면 `@ResponseBody` 어노테이션을 사용하여 컨트롤러 메소드가 JSON 형식으로 응답하도록 설정해야 합니다.

#### 예시 코드

 @GetMapping("/alimTalk/countAlimCenter")     
 @ResponseBody     public ResponseEntity<MyResponse> getCountAlimCenter() {         
 MyResponse response = new MyResponse();         
 // 결과 설정         
 return ResponseEntity.ok(response);     
 } 
 }`


### 주의사항

- `@RestController`를 사용하면 모든 메소드에 자동으로 `@ResponseBody`가 적용되어 JSON 응답을 쉽게 반환할 수 있다.
- Thymeleaf를 사용할 필요가 없는 상황에서는 꼭 `@ResponseBody`나 `@RestController`를 사용하는 것을 잊지 말기!

728x90
반응형
LIST