2016-03-04 35 views
0

这是Spring的初始化程序。我没有使用任何.xml文件。控制器寄存器,但返回数据时获得404

import org.springframework.context.annotation.Configuration; 
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 

import javax.servlet.ServletContext; 
import javax.servlet.ServletException; 

@Configuration 
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

    @Override 
    protected Class<?>[] getRootConfigClasses() { 
     return new Class<?>[]{ 
       WebAppConfig.class, 
       SecurityConfig.class, 
       DatabaseConfig.class, 
       DataSourceGenerator.class, 
       QuartzConfig.class, 
       QueueConfig.class 
     }; 
    } 

    @Override 
    public void onStartup(ServletContext servletContext) throws ServletException { 
     super.onStartup(servletContext); 
     servletContext.addFilter(
       "facilityFilter", new FacilityServletFilter() 
     ).addMappingForUrlPatterns(null, false, "/api/*"); 

     servletContext.addFilter(
       "hmacFilter", new HmacFilter() 
     ).addMappingForUrlPatterns(null, false, "/api/*"); 
    } 

    @Override 
    protected String[] getServletMappings() { 
     return new String[]{"/"}; 
    } 

    @Override 
    protected Class<?>[] getServletConfigClasses() { 
     return null; 
    } 
} 

这是我的控制器之一。

@Controller 
@RequestMapping(value = "/install") 
public class HelloController { 

    @RequestMapping(value = "/hi", method = RequestMethod.GET, 
      consumes = "*/*", produces = "text/html") 
    public String sayHello(){ 
     return "<html> <head> <title>API</title>" + 
       "</head>  <body>  <h1>Welcome to the Eric</h1>" + 
       "</body>  </html>"; 
    } 

} 

我所有的其他控制器似乎正常工作,但是当我尝试打端点这个方法返回一个404错误。当我通过Postman调用代码时,代码在调试器中被击中。

回答

0

添加@ResponseBody到控制器方法,否则弹簧会尝试寻找一个视图名称为"<html> <head> <title>API</title>..."

@Controller 
@RequestMapping(value = "/install") 
public class HelloController { 

    @RequestMapping(value = "/hi", method = RequestMethod.GET, consumes = "*/*", produces = "text/html") 
    @ResponseBody 
    public String sayHello(){ 
     return "<html> <head> <title>API</title>" + 
       "</head>  <body>  <h1>Welcome to the Eric</h1>" + 
       "</body>  </html>"; 
    } 

} 
0

至于建议的Raplh你可以这样做,但如果你计划有更多的这些方法可能以及只需将@Controller替换为@RestController