我正在尝试使用 Jetty 12.0.0.beta0 + Spring 6.0.4。但是,Spring dispatcher servlet 似乎没有在 http 请求上使用,所以我得到
HTTP ERROR 404 Not Found
.
这是我的代码:
@Order(Ordered.HIGHEST_PRECEDENCE)
public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
System.out.println("OnStartup method is called");//THIS MESSAGE IS PRINTED TO CONSOLE
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(MyController.class);
// Manage the lifecycle of the root application context
servletContext.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
//dispatcherContext.register(DispatcherConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher =
servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
}
}
@Controller
public class MyController {
@GetMapping(value = "/in", produces = MediaType.TEXT_HTML_VALUE)
public String in(Model model) {
System.out.println("in method");//THIS MESSAGE IS NOT PRINTED
var words = List.of("mountain", "noon", "rock", "river", "spring");
model.addAttribute("words", words);
return "index";
}
}
启动服务器后我得到:
2023-03-19 10:45:05.665 [DEBUG] [main] org.eclipse.jetty.util.component.AbstractLifeCycle - STARTING ContainerInitializer{org.springframework.web.SpringServletContainerInitializer,interested=[pk.temppro.server.web.ioc.WebInitializer],applicable=[],annotated=[]}
...
2023-03-19 10:45:05.711 [DEBUG] [main] org.eclipse.jetty.ee10.webapp.WebAppClassLoader - WAP webapp loaded class pk.temppro.server.web.controller.MyController
2023-03-19 10:45:05.714 [DEBUG] [main] org.eclipse.jetty.util.component.ContainerLifeCycle - oeje10s.ServletHandler@5ad40184{STOPPED} added {org.springframework.web.context.ContextLoaderListener@3abadb65{src=JAKARTA_API:<null>},AUTO}
2023-03-19 10:45:05.722 [DEBUG] [main] org.eclipse.jetty.util.component.ContainerLifeCycle - oeje10s.ServletHandler@5ad40184{STOPPED} added {dispatcher==org.springframework.web.servlet.DispatcherServlet@b7ba1aa7{jsp=null,order=-1,inst=false,async=false,src=JAKARTA_API:<null>,STOPPED},AUTO}
2023-03-19 10:45:05.723 [DEBUG] [main] org.eclipse.jetty.util.component.ContainerLifeCycle - oeje10s.ServletHandler@5ad40184{STOPPED} added {[/*]=>dispatcher,POJO}
2023-03-19 10:45:05.723 [DEBUG] [main] org.eclipse.jetty.ee10.servlet.ServletContainerInitializerHolder - ServletContainerInitializer org.springframework.web.SpringServletContainerInitializer called in 56ms
2023-03-19 10:45:05.724 [DEBUG] [main] org.eclipse.jetty.util.component.AbstractLifeCycle - STARTED @6822ms ContainerInitializer{org.springframework.web.SpringServletContainerInitializer,interested=[pk.temppro.server.web.ioc.WebInitializer],applicable=[],annotated=[]}
2023-03-19 10:45:05.726 [DEBUG] [main] org.eclipse.jetty.util.component.AbstractLifeCycle - STARTING org.springframework.web.context.ContextLoaderListener@3abadb65{src=JAKARTA_API:<null>}
2023-03-19 10:45:05.726 [DEBUG] [main] org.eclipse.jetty.util.component.ContainerLifeCycle - oeje10w.WebAppContext@13cf5f8a{ROOT,/,b=file:///home/pavel/Bin/TempPro/Server/temp/webserver/1.0.0/temppro-server/1.0.0/webapp/,a=STOPPED,h=oeje10s.SessionHandler@7b4619a3{STOPPED}}{/home/pavel/Bin/TempPro/Server/bin/./../repo/pk/temppro/temppro-server-web/1.0.0/temppro-server-web-1.0.0.war} added {org.springframework.web.context.ContextLoaderListener@6068cda1,POJO}
2023-03-19 10:45:05.727 [DEBUG] [main] org.eclipse.jetty.util.component.AbstractLifeCycle - STARTED @6825ms org.springframework.web.context.ContextLoaderListener@3abadb65{src=JAKARTA_API:<null>}
2023-03-19 10:45:05.751 [INFO] [main] org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
...
2023-03-19 10:45:05.760 [DEBUG] [main] org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Refreshing Root WebApplicationContext
...
2023-03-19 10:45:05.792 [DEBUG] [main] org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Registering component classes: [class pk.temppro.server.web.controller.MyController]
2023-03-19 10:45:05.842 [DEBUG] [main] org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
2023-03-19 10:45:05.875 [DEBUG] [main] org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
2023-03-19 10:45:05.877 [DEBUG] [main] org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
2023-03-19 10:45:05.878 [DEBUG] [main] org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
2023-03-19 10:45:05.879 [DEBUG] [main] org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
2023-03-19 10:45:05.884 [DEBUG] [main] org.springframework.ui.context.support.UiApplicationContextUtils - Unable to locate ThemeSource with name 'themeSource': using default [org.springframework.ui.context.support.ResourceBundleThemeSource@3987a1e8]
2023-03-19 10:45:05.885 [DEBUG] [main] org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'myController'
2023-03-19 10:45:05.908 [INFO] [main] org.springframework.web.context.ContextLoader - Root WebApplicationContext initialized in 156 ms
2023-03-19 10:45:05.910 [DEBUG] [main] org.eclipse.jetty.util.component.AbstractLifeCycle - STARTING jsp==org.eclipse.jetty.ee10.jsp.JettyJspServlet@19c47{jsp=null,order=0,inst=false,async=false,src=DESCRIPTOR:jar:file:///home/pavel/Bin/TempPro/Server/bin/./../repo/org/eclipse/jetty/ee10/jetty-ee10-webapp/12.0.0.beta0/jetty-ee10-webapp-12.0.0.beta0.jar!/org/eclipse/jetty/ee10/webapp/webdefault-ee10.xml,STOPPED}
2023-03-19 10:45:05.910 [DEBUG] [main] org.eclipse.jetty.ee10.servlet.BaseHolder - Holding class org.eclipse.jetty.ee10.jsp.JettyJspServlet from jdk.internal.loader.Loader@f80945f
2023-03-19 10:45:05.911 [DEBUG] [main] org.eclipse.jetty.util.component.AbstractLifeCycle - STARTED @7009ms jsp==org.eclipse.jetty.ee10.jsp.JettyJspServlet@19c47{jsp=null,order=0,inst=false,async=false,src=DESCRIPTOR:jar:file:///home/pavel/Bin/TempPro/Server/bin/./../repo/org/eclipse/jetty/ee10/jetty-ee10-webapp/12.0.0.beta0/jetty-ee10-webapp-12.0.0.beta0.jar!/org/eclipse/jetty/ee10/webapp/webdefault-ee10.xml,STARTED}
2023-03-19 10:45:05.913 [DEBUG] [main] org.eclipse.jetty.ee10.servlet.ServletHolder - Apache jasper detected
2023-03-19 10:45:05.914 [DEBUG] [main] org.eclipse.jetty.ee10.servlet.ServletHolder - Servlet.init null for jsp
...
2023-03-19 10:45:05.925 [DEBUG] [main] org.eclipse.jetty.ee10.servlet.ServletHolder - Servlet.init null for dispatcher
2023-03-19 10:45:05.926 [INFO] [main] org.eclipse.jetty.server.handler.ContextHandler.ROOT - Initializing Spring DispatcherServlet 'dispatcher'
2023-03-19 10:45:05.926 [INFO] [main] org.springframework.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcher'
2023-03-19 10:45:05.926 [DEBUG] [main] org.eclipse.jetty.jndi.InitialContextFactory - InitialContextFactory.getInitialContext()
2023-03-19 10:45:05.926 [DEBUG] [main] org.eclipse.jetty.jndi.InitialContextFactory - Created initial context delegate for local namespace:org.eclipse.jetty.jndi.local.localContextRoot@25109d84
2023-03-19 10:45:05.927 [DEBUG] [main] org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/spring.profiles.active]
...
2023-03-19 10:45:05.928 [DEBUG] [main] org.springframework.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/spring.profiles.active] not found - trying original name [spring.profiles.active]. javax.naming.NameNotFoundException; remaining name 'spring.profiles.active'
2023-03-19 10:45:05.928 [DEBUG] [main] org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [spring.profiles.active]
...
2023-03-19 10:45:05.929 [DEBUG] [main] org.springframework.jndi.JndiPropertySource - JNDI lookup for name [spring.profiles.active] threw NamingException with message: null. Returning null.
2023-03-19 10:45:05.930 [DEBUG] [main] org.springframework.core.env.PropertySourcesPropertyResolver - Found key 'spring.profiles.active' in PropertySource 'systemProperties' with value of type String
2023-03-19 10:45:05.930 [DEBUG] [main] org.springframework.web.context.support.StandardServletEnvironment - Activating profiles [production]
2023-03-19 10:45:05.930 [DEBUG] [main] org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/spring.profiles.default]
...
2023-03-19 10:45:05.931 [DEBUG] [main] org.springframework.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/spring.profiles.default] not found - trying original name [spring.profiles.default]. javax.naming.NameNotFoundException; remaining name 'spring.profiles.default'
2023-03-19 10:45:05.931 [DEBUG] [main] org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [spring.profiles.default]
...
2023-03-19 10:45:05.931 [DEBUG] [main] org.springframework.jndi.JndiPropertySource - JNDI lookup for name [spring.profiles.default] threw NamingException with message: null. Returning null.
2023-03-19 10:45:05.933 [DEBUG] [main] org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Refreshing WebApplicationContext for namespace 'dispatcher-servlet'
2023-03-19 10:45:05.934 [DEBUG] [main] org.eclipse.jetty.ee10.webapp.WebAppClassLoader - getResources META-INF/spring.components []
2023-03-19 10:45:05.934 [DEBUG] [main] org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
2023-03-19 10:45:05.935 [DEBUG] [main] org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
2023-03-19 10:45:05.935 [DEBUG] [main] org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
2023-03-19 10:45:05.935 [DEBUG] [main] org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
2023-03-19 10:45:05.936 [DEBUG] [main] org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
2023-03-19 10:45:05.936 [DEBUG] [main] org.springframework.ui.context.support.UiApplicationContextUtils - Unable to locate ThemeSource with name 'themeSource': using default [org.springframework.ui.context.support.DelegatingThemeSource@1abbc1d4]
2023-03-19 10:45:05.952 [DEBUG] [main] _org.springframework.web.servlet.HandlerMapping.Mappings - 'org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping' {}
2023-03-19 10:45:05.990 [DEBUG] [main] org.eclipse.jetty.ee10.webapp.WebAppClassLoader - getResources META-INF/services/javax.xml.transform.TransformerFactory []
2023-03-19 10:45:06.004 [DEBUG] [main] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter - ControllerAdvice beans: none
2023-03-19 10:45:06.004 [DEBUG] [main] org.eclipse.jetty.ee10.webapp.WebAppClassLoader - getResources META-INF/services/javax.xml.transform.TransformerFactory []
2023-03-19 10:45:06.027 [DEBUG] [main] org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - ControllerAdvice beans: none
2023-03-19 10:45:06.027 [DEBUG] [main] org.eclipse.jetty.ee10.webapp.WebAppClassLoader - getResources META-INF/services/javax.xml.transform.TransformerFactory []
2023-03-19 10:45:06.040 [DEBUG] [main] org.springframework.web.servlet.DispatcherServlet - enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
2023-03-19 10:45:06.040 [INFO] [main] org.springframework.web.servlet.DispatcherServlet - Completed initialization in 114 ms
...
抱歉,由于 SO 限制,我无法发布所有日志。当我在浏览器中打开
http://127.0.0.1:8080/in
我得到:
HTTP ERROR 404 Not Found
URI: http://127.0.0.1:8080/in
STATUS: 404
MESSAGE: Not Found
Powered by Jetty:// 12.0.0.beta0
处理请求时,日志消息中没有来自
org.springframework
的行。所有消息都来自org.eclipse.jetty
,没有任何错误
谁能告诉我怎么解决?当我使用 Jetty 10 和 Spring 5 时,一切正常。在 Jetty issue 中也提出了所有日志的问题。