栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring Boot:将另一个端口上的请求发送到自定义Servlet

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot:将另一个端口上的请求发送到自定义Servlet

要隔离

Connector
供单个应用程序使用的,该连接器需要与其自己的连接器关联,
Service
然后您需要
Context
将该应用程序的连接器与关联
Service

您可以在Spring Boot应用程序中通过提供您自己的

TomcatEmbeddedServletContainerFactory
子类a
@Bean
并进行覆盖来进行设置
getEmbeddedServletContainer(Tomcat tomcat)
。这使您有机会进行所需的配置更改:

@Beanpublic TomcatEmbeddedServletContainerFactory tomcatFactory() {    return new TomcatEmbeddedServletContainerFactory() {        @Override        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(     Tomcat tomcat) { Server server = tomcat.getServer(); Service service = new StandardService(); service.setName("other-port-service"); Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setPort(8081); service.addConnector(connector); server.addService(service); Engine engine = new StandardEngine(); service.setContainer(engine); Host host = new StandardHost(); host.setName("other-port-host"); engine.addChild(host); engine.setDefaultHost(host.getName()); Context context = new StandardContext(); context.addLifecycleListener(new FixContextListener()); context.setName("other-port-context"); context.setPath(""); host.addChild(context); Wrapper wrapper = context.createWrapper(); wrapper.setServlet(new MyServlet()); wrapper.setName("other-port-servlet"); context.addChild(wrapper); context.addServletMapping("/", wrapper.getName()); return super.getTomcatEmbeddedServletContainer(tomcat);        }    };}private static class MyServlet extends HttpServlet {    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        resp.getWriter().println("Hello, world");    }}

将此bean添加到您的应用程序后,应该由http://
localhost:8081处理

MyServlet
并返回包含“ Hello,world”的响应。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/443263.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号