Spring Boot、Thymeleaf 和 @Controller [英] Spring Boot, Thymeleaf and @Controller
问题描述
我在玩 Spring Boot 并且有一些我不太明白的东西.我的应用程序中有 2 个 @Controller
,而第二个并没有真正接收 REST 调用,而是 Thymeleaf 正在处理请求.
基本上我所拥有的是:
@Configuration@ComponentScan@EnableAutoConfiguration公共类应用{public static void main(String[] args) 抛出 Throwable {SpringApplication.run(Application.class, args);}}
然后
@Configuration@EnableWebMvcSecurity@启用网络安全@EnableGlobalMethodSecurity(prePostEnabled=true)公共类 SecurityConfig 扩展了 WebSecurityConfigurerAdapter {@自动连线环境环境;@覆盖protected void configure(HttpSecurity http) 抛出异常 {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();//为 angularjs 提供便利http.headers().frameOptions().disable();//对于H2网络控制台}}
和
@Configuration公共类 WebMvcConfig 扩展了 WebMvcConfigurerAdapter {@覆盖public void addViewControllers(ViewControllerRegistry 注册表) {registry.addViewController("/home").setViewName("home");registry.addViewController("/").setViewName("home");registry.addViewController("/hello").setViewName("hello");registry.addViewController("/login").setViewName("login");}@覆盖public void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/public/**").addResourceLocations("classpath:/public/");registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/");}}
还有两个控制器.这个有效,所以它从一个简单的 AngularJS 客户端接听我的电话并响应:
@Controller@RequestMapping("/foo")公共类 MyController {@RequestMapping(method = RequestMethod.GET)@ResponseBody@PreAuthorize("hasRole('ROLE_FOO')")公共字符串 getFoo() {返回 "foooooo";}}
这是生病的控制器,没有响应:
@Controller@RequestMapping("/sick/1")公共类 SickController {@自动连线SickRepositorysickRepository;@RequestMapping(method = RequestMethod.GET)公共生病 getSickById() {返回sickRepository.findOne(1);}}
显然,稍后我会将其更改为从 URL 中提取 ID 作为路径变量,但为了调试,我又回到了硬编码.
在我对 /sick/1
的请求到达之前,日志不会显示任何内容.那时我得到了这个:
org.thymeleaf.exceptions.TemplateInputException:解析模板sick/1"时出错,模板可能不存在或可能无法被任何配置的模板解析器访问在 org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:245)在 org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104)
但是为什么它会转到模板引擎而不是我的控制器..?
您可能在 getSickById
控制器方法上缺少 @ResponseBody
注释.
您还可以将 @Controller
注释替换为 @RestController
,Spring 会将 @ResponseBody
应用于该控制器中的所有控制器方法.>
I am playing around with Spring Boot and have something that I don't quite get. I have 2 @Controller
s in my application, and the second one is not really picking up REST calls, Thymeleaf is jumping on the requests instead.
Basically what I have is:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) throws Throwable {
SpringApplication.run(Application.class, args);
}
}
Then
@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
}
}
And
@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/");
}
}
And the two controllers. This one works, so it is picking up my call from a simple AngularJS client and responds:
@Controller
@RequestMapping("/foo")
public class MyController {
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
@PreAuthorize("hasRole('ROLE_FOO')")
public String getFoo() {
return "foooooo";
}
}
And this is the sick controller, not responding:
@Controller
@RequestMapping("/sick/1")
public class SickController {
@Autowired
SickRepository sickRepository;
@RequestMapping(method = RequestMethod.GET)
public Sick getSickById() {
return sickRepository.findOne(1);
}
}
Obviously later I'll change it to pull the ID from the URL as a path variable, but for debugging I went back to hardcoding.
The logs don't show anything until my request to /sick/1
arrive. At that point I am getting this:
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)
But why does it go to the template engine instead of my controller..?
You are probably missing @ResponseBody
annotation on the getSickById
controller method.
You could also replace @Controller
annotation with @RestController
and Spring will apply @ResponseBody
to all controller methods within that controller.
这篇关于Spring Boot、Thymeleaf 和 @Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!