这是Spring的初始化程序。我没有使用任何.xml文件。
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 :(得分:0)
将@ResponseBody
添加到您的控制器方法,否则spring会尝试搜索名为"<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>";
}
}
答案 1 :(得分:0)
根据Raplh的建议,您可以这样做但如果您计划更多这些方法,那么只需将@Controller
替换为@RestController