简体   繁体   English

在不同端口上注册 Sprint Boot Servlet

[英]Sprint Boot Servlet Registration on a different port

I have a spring-boot application with a @RestController, and need to register a servlet on a different port, while the RestController is bound to the default port.我有一个带有@RestController 的 spring-boot 应用程序,需要在不同的端口上注册一个 servlet,而 RestController 绑定到默认端口。

I have an application.yml file and a src>main>resources>application.properties file.我有一个 application.yml 文件和一个 src>main>resources>application.properties 文件。

This is my Servlet registration class这是我的 Servlet 注册 class

@Configuration
public class ServletConfig {
    private static final Logger LOGGER = LoggerFactory.getLogger (ServletConfig.class);

    @Bean
    public ServletRegistrationBean<MyServlet> servletRegistrationBean(final ServletContext servletContext){
        LOGGER.info("Inside ServletRegistrationBean");
        final ServletRegistrationBean <MyServlet> bean = new ServletRegistrationBean <> (new MyServlet (), true, "/endpoint");
        bean.setLoadOnStartup (1);
        return bean;
    }

Here is my MyServlet class:这是我的 MyServlet class:

public class MyServlet extends AbstractXServlet{ private static final Logger LOGGER = LoggerFactory.getLogger (MyServlet.class);公共 class MyServlet 扩展 AbstractXServlet{ 私有 static 最终 Logger LOGGER = LoggerFactory.getLogger (MyServlet.class);

public MyServlet() {
    LOGGER.info("Inside MyServlet Constructor");
    settings().setMultipartEnabled(false);
    final ServletHandlerr servletHandlerr = new ServletHandlerr();
    
    //Http POST Only
    handlerRegistry().registerHandler(EHttpMethod.POST, servletHandlerr);
}

} }

I want to define the port for the servlet on my application.properties file, different than the default port where all the RestController endpoints are.我想在我的 application.properties 文件中定义 servlet 的端口,与所有 RestController 端点所在的默认端口不同。 But right now, I can't get to print any of the log messages.但是现在,我无法打印任何日志消息。 Not sure what i'm doing wrong.不知道我做错了什么。 Any help is really appreciated.非常感谢任何帮助。

I want to define the port for the servlet on my application.properties file, different than the default port where all the RestController endpoints are我想在我的 application.properties 文件中定义 servlet 的端口,与所有 RestController 端点所在的默认端口不同

...which essentially means you want to have a separate web server, running on a different port than the rest of the application. ...这实质上意味着您想要一个单独的 web 服务器,在与应用程序的 rest 不同的端口上运行。 Personally, I would create a completely separate SpringBootApplication for that purpose.就个人而言,我会为此目的创建一个完全独立的SpringBootApplication

However, if you insist on bundling it together in a single app, you could create an additional embedded container that does not register the default servlet ( .setRegisterDefaultServlet(false) ) and instead only registers your custom servlet.但是,如果您坚持将其捆绑在一个应用程序中,您可以创建一个额外的嵌入式容器,该容器不注册默认 servlet ( .setRegisterDefaultServlet(false) ),而只注册您的自定义 servlet。

You will need to make sure your custom factory does not override the usual auto-configuration of the embedded container that the rest of the app will be bound to.您需要确保您的自定义工厂不会覆盖应用程序的 rest 将绑定到的嵌入式容器的通常自动配置。 You might need to duplicate parts of the original ServletWebServerFactoryConfiguration of the default servlet, and mark it @Primary to distinguish it from your custom servlet container.您可能需要复制默认 servlet 的原始ServletWebServerFactoryConfiguration的一部分,并将其标记为@Primary以将其与您的自定义 servlet 容器区分开来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM