SpringJava配置与JBoss7
我试图在jboss上运行一个基于SpringJava配置的简单应用程序,但没有成功。 该应用程序在jetty和tomcat上都可以正常工作。 jboss日志看起来不错,因为它向我展示了一些成功的映射等,但我在尝试访问url时遇到了404 这是我的代码: 初始值设定项SpringJava配置与JBoss7,java,spring,http-status-code-404,jboss7.x,config,Java,Spring,Http Status Code 404,Jboss7.x,Config,我试图在jboss上运行一个基于SpringJava配置的简单应用程序,但没有成功。 该应用程序在jetty和tomcat上都可以正常工作。 jboss日志看起来不错,因为它向我展示了一些成功的映射等,但我在尝试访问url时遇到了404 这是我的代码: 初始值设定项 @Order(1) public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override
@Order(1)
public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] {RootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] {WebAppConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
registration.setInitParameter("dispatchOptionsRequest", "true");
}
}
WebAppConfig
@Configuration
@ComponentScan("com.test")
@EnableWebMvc
@EnableSpringDataWebSupport
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Bean
public InternalResourceViewResolver setupViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
现在是jboss日志
17:08:53,645 INFO [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/test]] (MSC service thread 1-8) Spring WebApplicationInitializers detected on classpath: [br.com.cleartech.config.Initializer@2f7e4dd2]
17:08:53,767 INFO [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/test]] (MSC service thread 1-8) Initializing Spring root WebApplicationContext
17:08:53,768 INFO [org.springframework.web.context.ContextLoader] (MSC service thread 1-8) Root WebApplicationContext: initialization started
17:08:53,770 INFO [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (MSC service thread 1-8) Refreshing Root WebApplicationContext: startup date [Wed Dec 18 17:08:53 BRST 2013]; root of context hierarchy
17:08:53,843 INFO [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
17:08:53,846 INFO [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-330 'javax.inject.Named' annotation found and supported for component scanning
17:08:53,847 INFO [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (MSC service thread 1-8) Registering annotated classes: [class br.com.cleartech.config.RootConfig]
17:08:53,915 INFO [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
17:08:53,916 INFO [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-330 'javax.inject.Named' annotation found and supported for component scanning
17:08:53,977 INFO [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
17:08:53,978 INFO [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-330 'javax.inject.Named' annotation found and supported for component scanning
17:08:54,130 INFO [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (MSC service thread 1-8) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
17:08:54,153 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] (MSC service thread 1-8) Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@52477602: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,rootConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,log4j,webAppConfig,homeController,consoleAppender,fileAppender,registerSpringLogger,org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration,mvcValidator,simpleControllerHandlerAdapter,beanNameHandlerMapping,httpRequestHandlerAdapter,mvcContentNegotiationManager,requestMappingHandlerMapping,resourceHandlerMapping,requestMappingHandlerAdapter,mvcConversionService,viewControllerHandlerMapping,defaultServletHandlerMapping,handlerExceptionResolver,org.springframework.data.web.config.SpringDataWebConfiguration,pageableResolver,sortResolver,setupViewResolver]; root of factory hierarchy
17:08:54,269 INFO [org.hibernate.validator.util.Version] (MSC service thread 1-8) Hibernate Validator 4.2.0.Final
17:08:54,388 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-8) Mapped "{[/teste],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String br.com.cleartech.controller.HomeController.teste()
17:08:54,404 INFO [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-8) Mapped URL path [/resources/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
17:08:54,631 INFO [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-8) Root mapping to handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]
17:08:54,634 INFO [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-8) Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler]
17:08:54,668 INFO [org.springframework.web.context.ContextLoader] (MSC service thread 1-8) Root WebApplicationContext: initialization completed in 899 ms
17:08:54,675 INFO [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/test]] (MSC service thread 1-8) Initializing Spring FrameworkServlet 'dispatcher'
17:08:54,676 INFO [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-8) FrameworkServlet 'dispatcher': initialization started
17:08:54,679 INFO [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (MSC service thread 1-8) Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Wed Dec 18 17:08:54 BRST 2013]; parent: Root WebApplicationContext
17:08:54,681 INFO [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
17:08:54,682 INFO [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-330 'javax.inject.Named' annotation found and supported for component scanning
17:08:54,683 INFO [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (MSC service thread 1-8) Registering annotated classes: [class br.com.cleartech.config.WebAppConfig]
17:08:54,685 INFO [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
17:08:54,686 INFO [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-330 'javax.inject.Named' annotation found and supported for component scanning
17:08:54,698 INFO [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
17:08:54,699 INFO [org.springframework.context.annotation.ClassPathBeanDefinitionScanner] (MSC service thread 1-8) JSR-330 'javax.inject.Named' annotation found and supported for component scanning
17:08:54,759 INFO [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (MSC service thread 1-8) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
17:08:54,786 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] (MSC service thread 1-8) Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@12f882f3: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,webAppConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,log4j,rootConfig,homeController,consoleAppender,fileAppender,registerSpringLogger,org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration,mvcValidator,simpleControllerHandlerAdapter,beanNameHandlerMapping,httpRequestHandlerAdapter,mvcContentNegotiationManager,requestMappingHandlerMapping,resourceHandlerMapping,requestMappingHandlerAdapter,mvcConversionService,viewControllerHandlerMapping,defaultServletHandlerMapping,handlerExceptionResolver,org.springframework.data.web.config.SpringDataWebConfiguration,pageableResolver,sortResolver,setupViewResolver]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@52477602
17:08:54,885 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-8) Mapped "{[/teste],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String br.com.cleartech.controller.HomeController.teste()
17:08:54,891 INFO [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-8) Mapped URL path [/resources/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
17:08:54,927 INFO [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-8) Root mapping to handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]
17:08:54,931 INFO [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-8) Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler]
17:08:54,962 INFO [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-8) FrameworkServlet 'dispatcher': initialization completed in 285 ms
17:08:54,977 INFO [org.jboss.web] (MSC service thread 1-8) JBAS018210: Registering web context: /test
17:08:54,985 INFO [org.jboss.as] (MSC service thread 1-5) JBAS015951: Admin console listening on http://127.0.0.1:9990
17:08:54,986 INFO [org.jboss.as] (MSC service thread 1-5) JBAS015874: JBoss AS 7.1.1.Final "Brontes" started in 8333ms - Started 377 of 455 services (77 services are passive or on-demand)
17:08:55,175 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "test.war"
正如你所看到的,这似乎是好的,因为我得到了一些:
Spring WebApplicationInitializers detected on classpath
Initializing Spring root WebApplicationContext
Root WebApplicationContext: initialization started
Mapped URL path [/resources/**]
Mapped URL path [/**]
Registering web context: /test
JBAS018559: Deployed "test.war"
但当试图访问localhost:8080/test,甚至是在控制器中声明的一个简单rest时,我得到了404
编辑
我只是想解释一下,在写这篇文章的时候,我并没有使用SpringBoot。大多数答案都将其视为一种解决方案。好吧,对于其他面临此问题的人来说,它在新的Wildfly上运行良好。
如果您没有任何特定的容器来运行您的应用程序,您可以在JBoss7和wildfly之间进行选择,并希望运行SpringJavaConfig,请在wildfly上进行尝试 部署到JBoss7.1的Spring MVC项目没有web.xml,我也遇到了类似的问题 根据SpringJavadocsforWebApplicationInitializer,Tomcat的旧版本(我们在JBossEAP6.2上有一个SpringBoot(1.1.4)项目(我客户的要求…) 我找到了一个在JBossEAP6.2.0GA上运行它的解决方案,并保持了在ApacheTomcat7容器上运行的能力 最初,我的项目以嵌入式模式运行,所以我需要创建并更改一些文件以在容器上运行 要作为根应用程序在Tomcat上运行,我创建了context.xml: /src/main/webapp/META-INF/context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path=""/>
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<context-root>/</context-root>
</jboss-web>
不要忘记在启动时调用super.on(容器);
pom.xml中的更改:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
export JAVA_OPTS="-Dspring.profiles.active=local"
.../jboss-eap-6.2/bin/standalone.sh
如果在Tomcat上运行,不要忘记设置-Dspring.profiles.active=local
正如我看到的,在容器上运行时,server.port设置将被忽略。根据Michael R和István Pató提供的答案,JBoss中的servlet映射必须是“/*”,而不是“/”。但是,其他解决方案会导致@Component注释对象被实例化两次。下面通过首先调用
super.onStartup
,然后为dispatcher servlet添加另一个映射来解决双重初始化问题:
public class WebApplicationInitializerImpl implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
super.onStartup(container);
Dynamic registration = (Dynamic) container.getServletRegistration(EmbeddedWebApplicationContext.DISPATCHER_SERVLET_NAME);
registration.setLoadOnStartup(1);
registration.addMapping("/*");
}
}
我正在使用@springbootplication 当我读到这篇文章时,我需要: 将DispatcherServlet的映射更改为“/*”而不是“/”(通过添加ServletRegistrationBean类型的@Bean和名为“DispatcherServlet”的servlet) 在这个url中,我找到了代码解决方案:
我正在使用JBoss EAP 6.4。我正在浏览线程 在将dispatchServlet的映射从“/”更改为“/*”之后,我想补充一点。您项目中的JSP可能无法正确处理。我怀疑由于“/*”比“/*.JSP”具有更高的优先级因此,JSPServlet可能无法获得处理JSP的请求,JSP将无法正确处理 我通过在web.xml中将JSP定义为servlet解决了这个问题,如下所述
index.jsp
罗金瑟夫莱特酒店
/login.jsp
IndexServlet
/index.jsp
罗金瑟夫莱特酒店
/login.jsp
IndexServlet
/index.jsp
我在这方面花了很多时间,可能会帮助一些人:)我正在使用Spring Boot 1.3.1和JBoss EAP 6.4。我发现在您的项目中,您可以添加到src/main/resources/application.properties这一行:
server.servlet-path=/*
另外,如果您是从Eclipse中启动此项目,请确保清理您的项目。。。我浪费了很多时间,因为这一点。我在JBoss6.4.0上也遇到了类似的问题。弹簧靴1.3与Tiles 3结合使用。安装Jboss补丁Jboss-eap-6.4.6-patch.zip后,问题得到了解决
打补丁后,我不需要使用设置服务器。servlet路径=/*当您尝试访问管理控制台时会发生什么athttp://127.0.0.1:9990 ?你有
/
的处理程序吗?@Nambari,工作正常,出现在jboss管理控制台上…@Sotirios,我有一个:public void addviewcontrollerregistry(ViewControllerRegistry registry){registry.addViewController(“/”).setViewName(“index”);}@SotiriosDelimanolis:我怀疑,其他服务(jetty/tomcat)在8080上运行,但应用程序不在那里,不管怎样,从日志中似乎可以清楚地看出Jboss正在8080上侦听。如果没有运行8080的web服务器,您将得到没有响应的浏览器屏幕。它在EAP 6.2上也适用于我(我认为是7.3)。问题似乎是“/”(默认servlet)映射没有被JBoss 7.1视为隐式的(但Tomcat和Jetty是这样),但只有在使用Java配置而不是web.xml.wird.Correction时,它才适用于EAP 6.2。JBoss AS 7是一个dud.Wildfly(AS 8)对我有效。或者您可以在AS 7中处理容器配置(例如在standalone.xml
中设置enable welcome root=“false”
)。调用newAnnotationConfigWebApplicationContext()SpringBootServletInitializer
子类中的
导致所有@Component
注释对象实例化两次(请参阅我的答案以了解如何避免此问题)@thiago pereira,我想恭敬地说,这个答案不适合所问的问题。这个问题是关于Spring Java配置的,而不是关于Spring Boot。我相信我的答案仍然能够最好地解决Spring Java配置的问题。我还更新了它,以解决JSP的问题。调用newAnnotationConfigWebApplicationCoSpringBootServletInitializer
子类中的ntext()
导致所有@Component
注释对象实例化两次(请参阅我的答案以了解如何避免此问题)。需要注意的是,您的问题仅适用于Spring Boot。对于非启动Spring MVC应用程序,此解决方案应该可以。在编写此解决方案时,我不是usi
export JAVA_OPTS="-Dspring.profiles.active=local"
.../jboss-eap-6.2/bin/standalone.sh
public class WebApplicationInitializerImpl implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
super.onStartup(container);
Dynamic registration = (Dynamic) container.getServletRegistration(EmbeddedWebApplicationContext.DISPATCHER_SERVLET_NAME);
registration.setLoadOnStartup(1);
registration.addMapping("/*");
}
}
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Bean
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
/**
* Register dispatcherServlet programmatically
*
* @return ServletRegistrationBean
*/
@Bean
public ServletRegistrationBean dispatcherServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(
dispatcherServlet(), "/*");
registration
.setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);
return registration;
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<jsp-file>/login.jsp</jsp-file>
</servlet>
<servlet>
<servlet-name>IndexServlet</servlet-name>
<jsp-file>/index.jsp</jsp-file>
</servlet>
<!--mapping -->
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>IndexServlet</servlet-name>
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>
server.servlet-path=/*