You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用REST接口后ThymeLeaf视图无法显示问题求助

Troubleshooting Thymeleaf Template Not Rendering for /devices Endpoint

Let's walk through the most likely fixes for your issue, since you confirmed /deviceType works fine (ruling out global config/security problems):

1. Check Your Controller Annotation (Top Culprit)

The most common reason your method returns a raw string instead of rendering the template is using @RestController instead of @Controller on your devices controller class.

@RestController automatically adds @ResponseBody to all methods, which tells Spring to serialize the return value directly as the HTTP response body (hence you get the string "deviceList" instead of triggering the view resolver).

Compare this to your working /deviceType endpoint—its controller is almost certainly using @Controller. Swap the annotation on your devices controller, and this should fix the issue immediately.

2. Verify Template File Location & Naming

Double-check that your deviceList.html is placed in Spring Boot's default Thymeleaf template directory:
src/main/resources/templates/

Also confirm the filename matches exactly (case-sensitive on most OS):

  • If your template is named DeviceList.html or device_list.html, returning "deviceList" won't locate it.
  • Mirror the path and naming of your working deviceType template to ensure consistency.

3. Check Custom View Resolver Config (If You Added One)

If you manually configured a ViewResolver bean, make sure it's set up to resolve Thymeleaf templates correctly. The default Spring Boot config handles this automatically, but if you overrode it, verify:

  • The prefix is set to classpath:/templates/
  • The suffix is set to .html

Example of a correct Thymeleaf view resolver setup:

@Bean
public ThymeleafViewResolver viewResolver() {
    ThymeleafViewResolver resolver = new ThymeleafViewResolver();
    resolver.setTemplateEngine(templateEngine());
    resolver.setCharacterEncoding("UTF-8");
    return resolver;
}

@Bean
public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine engine = new SpringTemplateEngine();
    engine.setTemplateResolver(templateResolver());
    return engine;
}

@Bean
public ITemplateResolver templateResolver() {
    SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
    resolver.setPrefix("classpath:/templates/");
    resolver.setSuffix(".html");
    resolver.setTemplateMode(TemplateMode.HTML);
    resolver.setCharacterEncoding("UTF-8");
    return resolver;
}

4. Enable Debug Logging to Diagnose

If none of the above works, turn on debug logging for Spring's view resolution to see exactly what's happening. Add these lines to your application.properties:

logging.level.org.springframework.web.servlet=DEBUG
logging.level.org.thymeleaf=DEBUG

When you hit /devices, the logs will show which templates Spring is attempting to resolve, and why it's falling back to returning the raw string.


内容的提问来源于stack exchange,提问作者Pete B.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:34:57