【问题标题】:Spring Boot, Thymeleaf and @ControllerSpring Boot、Thymeleaf 和 @Controller
【发布时间】:2014-12-12 20:01:47
【问题描述】:

我正在使用 Spring Boot 并且有一些我不太了解的东西。我的应用程序中有 2 个@Controllers,第二个并没有真正接收 REST 调用,Thymeleaf 正在处理请求。

基本上我拥有的是:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
     public static void main(String[] args) throws Throwable {
            SpringApplication.run(Application.class, args);
     }
}

然后

@Configuration
@EnableWebMvcSecurity
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    Environment env;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
          .antMatchers("/", "/home").permitAll()
          .antMatchers("/webjars/**").permitAll()
          .antMatchers("/console/**").permitAll()
          .antMatchers("/resources/**").permitAll()
          .anyRequest().authenticated();
        http.formLogin().loginPage("/login").permitAll().and().logout()
                .permitAll();
        http.csrf().disable(); // for angularjs ease
        http.headers().frameOptions().disable(); //for H2 web console
    }
}

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/home").setViewName("home");
        registry.addViewController("/").setViewName("home");
        registry.addViewController("/hello").setViewName("hello");
        registry.addViewController("/login").setViewName("login");
    }

    @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/public/**").addResourceLocations("classpath:/public/");
        registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/");
      }

}

还有两个控制器。这个有效,所以它从一个简单的 AngularJS 客户端接听我的电话并做出响应:

@Controller
@RequestMapping("/foo")
public class MyController {

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    @PreAuthorize("hasRole('ROLE_FOO')")
    public String getFoo() {
        return "foooooo";
    }
}

这是生病的控制器,没有响应:

@Controller
@RequestMapping("/sick/1")
public class SickController {

    @Autowired
    SickRepository sickRepository;

    @RequestMapping(method = RequestMethod.GET)
    public Sick getSickById() {
        return sickRepository.findOne(1);
    }

}

显然,稍后我会将其更改为从 URL 中提取 ID 作为路径变量,但为了调试,我又回到了硬编码。

在我对/sick/1 的请求到达之前,日志不会显示任何内容。那时我得到了这个:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "sick/1", template might not exist or might not be accessible by any of the configured Template Resolvers
    at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:245)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104)

但是为什么它会转到模板引擎而不是我的控制器..?

【问题讨论】:

    标签: java spring spring-mvc spring-boot


    【解决方案1】:

    您可能在getSickById 控制器方法上缺少@ResponseBody 注释。

    您还可以将@Controller 注释替换为@RestController,Spring 会将@ResponseBody 应用于该控制器中的所有控制器方法。

    【讨论】:

    • 好的,我知道了。所以我的控制器方法返回了一些东西,Spring 想要进一步解析为一个视图,对吧?并且该注释告诉不要,只返回该方法产生的任何内容。
    • @jabal 是的,这是正确的。 ResponseBody 注释告诉 Spring 在响应中写入方法的返回值,而不是将其解析为视图。我建议查看这篇文章 javacodegeeks.com/2013/07/… 或 Spring 文档以获取更多详细信息。这背后有更多的逻辑。例如,当您使用 ResponseBody 时,Spring 会使用 HttpMessageConverters 等将返回值转换为 JSON 或 XML 等等...
    猜你喜欢
    • 2020-08-16
    • 1970-01-01
    • 2019-10-31
    • 2021-08-02
    • 2019-03-25
    • 2017-05-26
    • 2019-06-28
    • 2020-11-26
    • 2016-09-23
    相关资源
    最近更新 更多