ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-Rest控制器上的Spring Boot Thymeleaf ViewResolver

2019-11-12 02:20:03  阅读:232  来源: 互联网

标签:spring-boot thymeleaf spring java spring-mvc


问候

我有一个Spring Boot应用程序(1.4.1版).
以前从

<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-支持百里香模板中不严格的html.
我仅将Thymeleaf模板用于电子邮件模板.该应用程序代表REST API,所有控制器均返回json数据.

但是,在为电子邮件设置Thymeleaf之后,一些请求正在为其寻找Thymeleaf模板并返回代码500.

Thymeleaf配置(yml,这是百里香的所有配置,没有其他JAVA配置,Spring Boot处理所有内容):

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

示例控制器和错误:

@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和前端.
特别是该控制器没有被注释为@RestController.固定.

标签:spring-boot,thymeleaf,spring,java,spring-mvc
来源: https://codeday.me/bug/20191112/2024006.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有