6

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..?

1 Answer 1

18

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I got it now. So my controller method returned something, that Spring wanted to further resolve to a View, right? And that annotation tells not to, just return whatever comes out of the method.
@jabal Yes that is correct. The ResponseBody annotation tells Spring to write the return value of your method in the response rather than to resolve it to a view. I recommend to take a look at this article javacodegeeks.com/2013/07/… or Spring documentation for further details. There is more logic behind this. For example when you use ResponseBody the return value is converted by Spring for instance to JSON or XML using HttpMessageConverters and so on ...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.