Spring Boot,Thymeleaf和@Controller

jab*_*bal 4 java spring spring-mvc spring-boot

我正在玩Spring Boot并且有一些我不太了解的东西.我的@Controller应用程序中有2 秒,而第二个并没有真正接受REST调用,Thymeleaf正在跳转请求.

基本上我所拥有的是:

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

然后

@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
    }
}
Run Code Online (Sandbox Code Playgroud)

@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/");
      }

}
Run Code Online (Sandbox Code Playgroud)

和两个控制器.这个工作正常,所以它从一个简单的AngularJS客户端接听我的电话并响应:

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

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

这是病态的控制器,没有回应:

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

    @Autowired
    SickRepository sickRepository;

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

}
Run Code Online (Sandbox Code Playgroud)

显然稍后我会改变它以从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)
Run Code Online (Sandbox Code Playgroud)

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

Boh*_*rdt 17

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

您也可以@Controller使用@RestControllerSpring 替换注释,Spring将应用于@ResponseBody该控制器中的所有控制器方法.

  • @jabal是的,这是正确的.ResponseBody注释告诉Spring在响应中写入方法的返回值,而不是将其解析为视图.我建议您查看这篇文章http://www.javacodegeeks.com/2013/07/spring-mvc-requestbody-and-responsebody-demystified.html或Spring文档以获取更多详细信息.这背后有更多的逻辑.例如,当您使用ResponseBody时,返回值由Spring转换为例如使用HttpMessageConverters的JSON或XML等等...... (4认同)