【发布时间】:2015-06-25 01:15:30
【问题描述】:
我正在尝试将第 3 方 servlet 集成到我的 Spring Boot 应用程序中,当我尝试向 servlet 提交 POST 时,我在日志中看到以下内容:
PageNotFound: Request method 'POST' not supported
我做了一个简单的测试来证明这一点。我开始使用auto generated Spring Boot project。然后我创建了以下 Servlet:
public class TestServlet extends HttpServlet {
private static final Logger log = LoggerFactory.getLogger(TestServlet.class);
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp); //To change body of generated methods, choose Tools | Templates.
log.info("doPost was called!!");
}
}
然后我像这样创建了配置:
@Configuration
public class ServletConfig {
@Bean //exposes the TestServlet at /test
public Servlet test() {
return new TestServlet();
}
}
然后我在 Tomcat7 中运行该应用程序。我在日志中看到:
ServletRegistrationBean: Mapping servlet: 'test' to [/test/]
然后我尝试像这样使用 cUrl 命中端点:
curl -v http://localhost:8080/test -data-binary '{"test":true}'
或
curl -XPOST -H'Content-type: application/json' http://localhost:8080/test -d '{"test":true}'
我尝试添加@RequestMapping,但这也不起作用。谁能帮我弄清楚如何在我的 Spring Boot 应用程序中支持另一个 Servlet?
您可以在此处找到示例应用程序:https://github.com/andrewserff/servlet-demo
谢谢!
【问题讨论】:
标签: java spring spring-mvc servlets spring-boot