gpt4 book ai didi

java - Rest Controller 上的 Spring Boot Thymeleaf ViewResolver

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:36:52 30 4
gpt4 key购买 nike

问候。

我有一个 Spring Boot 应用程序(1.4.1 版)。之前设置了来自

Thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>

nekohtml - 支持 thymeleaf 模板中不严格的 html。我仅将 Thymeleaf 模板用于电子邮件模板。该应用代表 REST API,所有 Controller 都返回 json 数据。

然而,在我为电子邮件设置 Thymeleaf 后,一些请求正在为它们寻找 Thymeleaf 模板并返回 code 500

Thymeleaf 配置(yml,这是 thymeleaf 的所有配置,没有其他 JAVA 配置,Spring Boot 处理一切):

  thymeleaf:
check-template-location: true
prefix: classpath:/templates/
suffix: .html
mode: LEGACYHTML5
encoding: UTF-8
content-type: text/html
cache: true

示例 Controller 和错误:

@RequestMapping(value = "/register", method = RequestMethod.POST)
public JsonResponse AddUser(@RequestBody @Valid User user, WebRequest request) throws SQLException {
String result = userService.RegisterUser(user);
if(result.equals("done")) {
try {
eventPublisher.publishEvent(new OnRegistrationCompleteEvent(user, request.getLocale()));
} catch (Exception me) {
return new JsonResponse("FAIL", "Unknown on event publishing: "+ me.getMessage());
}
return new JsonResponse("OK", "");

} else if(result.equals("duplicate")) {
return new JsonResponse("FAIL", "duplicate");
}
return new JsonResponse("FAIL", "Unknown");
}

错误:

2016-11-25 11:02:04.285 DEBUG 15552 --- [nio-8080-exec-1] o.s.s.w.a.i.FilterSecurityInterceptor    : Authorization successful
2016-11-25 11:02:04.285 DEBUG 15552 --- [nio-8080-exec-1] o.s.s.w.a.i.FilterSecurityInterceptor : RunAsManager did not change Authentication object
2016-11-25 11:02:04.285 DEBUG 15552 --- [nio-8080-exec-1] o.s.security.web.FilterChainProxy : /register reached end of additional filter chain; proceeding with original chain
2016-11-25 11:02:04.289 DEBUG 15552 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing POST request for [/api/register]
2016-11-25 11:02:04.291 DEBUG 15552 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /register
2016-11-25 11:02:04.294 DEBUG 15552 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public com.springapp.models.common.JsonResponse com.springapp.controllers.api.IndexController.AddUser(com.springapp.models.common.User,org.springframework.web.context.request.WebRequest) throws java.sql.SQLException]
2016-11-25 11:02:04.329 DEBUG 15552 --- [nio-8080-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Read [class com.springapp.models.common.User] as "application/json;charset=UTF-8" with [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@682ef707]
2016-11-25 11:02:15.620 DEBUG 15552 --- [nio-8080-exec-1] o.s.w.servlet.view.BeanNameViewResolver : No matching bean found for view name 'register'
2016-11-25 11:02:15.635 DEBUG 15552 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Rendering view [org.thymeleaf.spring4.view.ThymeleafView@1a462947] in DispatcherServlet with name 'dispatcherServlet'
2016-11-25 11:02:15.647 ERROR 15552 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "register": Error resolving template "register", template might not exist or might not be accessible by any of the configured Template Resolvers
2016-11-25 11:02:15.651 DEBUG 15552 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Error rendering view [org.thymeleaf.spring4.view.ThymeleafView@1a462947] in DispatcherServlet with name 'dispatcherServlet'

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "register", template might not exist or might not be accessible by any of the configured Template Resolvers

最佳答案

对不起大家,那是我的错误。以前我将应用程序从 Spring MVC 迁移到 Spring Boot,并将其拆分为 REST API 和前端。特别是该 Controller 未被注释为 @RestController。固定。

关于java - Rest Controller 上的 Spring Boot Thymeleaf ViewResolver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40797628/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com