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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With