Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot, Thymeleaf and @Controller

I am playing around with Spring Boot and have something that I don't quite get. I have 2 @Controllers 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..?

like image 790
jabal Avatar asked Dec 12 '14 20:12

jabal


1 Answers

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.

like image 104
Bohuslav Burghardt Avatar answered Sep 25 '22 10:09

Bohuslav Burghardt