2015-06-06 26 views
1

在Spring Boot中,我添加了@Configuration註釋@EnableWebMvc爲自定義MVC。它使用src/main/resources/templates下的Freemarker模板。問題在於登錄頁面正在以瀏覽器空白的json的形式返回。我是否需要添加額外的內容協商或其他內容? THX春季引導發送空JSON爲自定義FreeMarker登錄頁

@Configuration 
@EnableWebMvc 
public class WebMvcConfig extends WebMvcConfigurerAdapter { 

@Bean 
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { 
    return new PropertySourcesPlaceholderConfigurer(); 
} 

@Bean 
public ContentNegotiatingViewResolver contentViewResolver() throws Exception { 
    ContentNegotiationManagerFactoryBean contentNegotiationManager = new ContentNegotiationManagerFactoryBean(); 
    contentNegotiationManager.addMediaType("json", MediaType.APPLICATION_JSON); 

    FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver(); 
    viewResolver.setPrefix("/templates/"); 
    viewResolver.setSuffix(".ftl"); 

    MappingJackson2JsonView defaultView = new MappingJackson2JsonView(); 
    defaultView.setExtractValueFromSingleKeyModel(true); 

    ContentNegotiatingViewResolver contentViewResolver = new ContentNegotiatingViewResolver(); 
    contentViewResolver.setContentNegotiationManager(contentNegotiationManager.getObject()); 
    contentViewResolver.setViewResolvers(Arrays.<ViewResolver> asList(viewResolver)); 
    contentViewResolver.setDefaultViews(Arrays.<View> asList(defaultView)); 
    return contentViewResolver; 
} 
    .... 

}

@Controller 
@SessionAttributes 
public class LoginController { 

    @RequestMapping("/login") 
    public String login() { 
     return "login"; 
    } 
} 

回答

0

登錄控制器缺少用於客戶端的HTTP適當的內容類型設置GET,所以我增加這產生屬性。 @RequestMapping(value =「/ login」,產生=「application/xml」)