2014-12-12 10 views
3

나는 Spring Boot로 놀고 있는데,별로 얻지 못하는 것을 가지고있다. 내 응용 프로그램에서 2 @Controller s 가지고 있고 두 번째 정말 REST 호출을 선택하지 않는, Thymeleaf 요청에 대신 점프입니다.Spring Boot, Thymeleaf, @Controller

기본적으로 내가 가지고있는 것은 :

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

그런

@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 
    } 
} 

그리고

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

} 

그리고 두 개의 컨트롤러. 이것은 작품, 그래서 간단한 AngularJS와 클라이언트에서 내 전화를 따기 및 응답 :

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

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

을 그리고이 응답하지 병자 컨트롤러입니다 : 나중에 내가 그것을 바꿀거야 분명히

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

    @Autowired 
    SickRepository sickRepository; 

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

} 

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) 

을하지만, 왜 내 컨트롤러 대신 템플릿 엔진으로 이동 않습니다 ..?

답변

13

컨트롤러 방법이 getSickById 인 경우 @ResponseBody 주석이 누락되었을 수 있습니다.

주석을 @RestController으로 바꿀 수 있으며 Spring은 해당 컨트롤러 내의 모든 컨트롤러 메소드에 @ResponseBody을 적용합니다.

+0

좋아, 이제 알았어. 그래서 내 컨트롤러 메소드는 뭔가를 돌려 주었고, Spring은 View로 더 나아가고 싶었다. 맞습니까? 그리고 그 주석은하지 말고, 그 방법에서 나오는 것은 무엇이든 반환하십시오. – jabal

+4

@jabal 네, 맞습니다. ResponseBody 주석은 Spring에게 메소드의 리턴 값을 뷰로 해석하지 않고 응답으로 작성하도록 지시합니다. 자세한 내용은이 기사 http://www.javacodegeeks.com/2013/07/spring-mvc-requestbody-and-responsebody-demystified.html 또는 Spring 설명서를 참조하는 것이 좋습니다. 이것의 뒤에는 더 많은 논리가 있습니다. 예를 들어 ResponseBody를 사용하면 반환 값은 HttpMessageConverters를 사용하여 JSON 또는 XML로 변환됩니다. –

관련 문제