Spring Java Config vs Jboss 7

J’essaie de lancer une application simple avec une configuration basée sur Java java sur jboss, mais sans succès. Cette application fonctionne très bien aussi bien sur la jetée que sur tomcat. Le journal jboss semble bon, car il me montre quelques correspondances réussies, etc., mais 404 essayant d’accéder à l’URL.

Voici mon code:

Initialiseur

@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 Ssortingng[] getServletMappings() { return new Ssortingng[] {"/"}; } @Override protected void customizeRegistration(ServletRegistration.Dynamic registration) { registration.setInitParameter("dispatchOptionsRequest", "true"); } } 

RootConfig

 @Configuration @ComponentScan(value = "com.test.config", excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, value = RootConfig.class)) public class RootConfig { } 

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(); } } 

Et maintenant, le journal de 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.Ssortingng 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.Ssortingng 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" 

Comme vous pouvez le constater, cela semble aller, puisque j’en ai:

 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" 

Mais en essayant d’accéder à localhost: 8080 / test ou même un simple repos déclaré dans un contrôleur, j’ai 404.

Merci d’avance Thiago

J’ai eu un problème similaire avec un projet Spring MVC déployé sur JBoss 7.1 sans web.xml.

Selon Spring javadocs for WebApplicationInitializer, les anciennes versions de Tomcat (<= 7.0.14) ne pouvaient pas être mappées sur "/" par programme. Les anciennes versions de JBoss AS 7 présentent le même défaut.

C’était la source de mon problème. J’enregistrais le servlet via “/”, mais JBoss EAP 6.4 ne prend pas en charge ce mappage par programme. Cela ne fonctionne que via web.xml. Je voulais toujours utiliser la configuration programmatique, alors j’ai changé le mappage en “/ *” au lieu de “/”, et cela a résolu mon problème.

 public class WebApplicationInitializerImpl implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) throws ServletException { WebApplicationContext context = getContext(); Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(context)); registration.setLoadOnStartup(1); registration.addMapping("/*"); } private WebApplicationContext getContext() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation(AppConfig.class.getName()); return context; } } 

Remarque: cette configuration est incompatible avec les vues JSP. “/ *” remplacera le servlet JSP du conteneur de servlet. Si vous utilisez encore les vues JSP, je vous recommande d’utiliser web.xml pour configurer DispatcherServlet plutôt que par programme. la configuration web.xml fonctionne correctement avec “/”.

  dispatcher org.springframework.web.servlet.DispatcherServlet 1  contextConfigLocation     dispatcher /  

J’utilisais @SpringBootApplication

Comme je l’ai lu dans ce fil j’avais besoin de:

Modifiez le mappage du DispatcherServlet en “/ *” au lieu de “/” (en ajoutant un @Bean de type ServletRegistrationBean avec un servlet nommé “dispatcherServlet”)

Dans cette url, j’ai trouvé la solution de code: Ajouter un mappage de servlet pour expédier un servlet

 @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(Ssortingng[] args) { SpringApplication.run(Application.class, args); } } 

Nous avons un projet Spring-Boot (1.1.4) sur JBoss EAP 6.2 (l’exigence de mon client …)

J’ai trouvé une solution pour l’exécuter sur GA JBoss EAP 6.2.0 et conserver la capacité de fonctionner sur un conteneur Apache Tomcat 7.

Initialement, mon projet s’exécutait en mode intégré; je devais donc créer et modifier des fichiers afin de les exécuter sur des conteneurs.

Pour exécuter l’application Tomcat en tant qu’application racine, j’ai créé le fichier context.xml : /src/main/webapp/META-INF/context.xml.

   

Pour fonctionner sur JBOSS EAP 6.2.0 GA en tant qu’application racine, j’ai créé jboss-web.xml: /src/main/webapp/WEB-INF/jboss-web.xml

   /  

J’ai créé une classe, car le mappage de servlets JBoss fonctionne comme / * mais pas avec / :

 import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration.Dynamic; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.web.SpringBootServletInitializer; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; /** * Working without web.xml with container (not em,bedded mode). * JBOSS EAP 6.2 specific: you need to map dispatcherServlet to /* . */ public class ContainerWebXml extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(TomcatStart.class); } /** * JBOSS EAP 6.2 mapping. * * @param container * @throws ServletException */ @Override public void onStartup(ServletContext container) throws ServletException { WebApplicationContext context = getContext(); Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(context)); registration.setLoadOnStartup(1); registration.addMapping("/*"); // required JBOSS EAP 6.2.0 GA super.onStartup(container); } private WebApplicationContext getContext() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation(TomcatStart.class.getName()); return context; } } 

N’oubliez pas d’appeler super.onStartup (conteneur);

Changements dans pom.xml:

   org.springframework.boot spring-boot-starter-tomcat provided  

Si vous utilisez spring.profile, vous devez définir env. variable.

J’exécute JBoss EAP 6.2.0 GA en mode autonome:

 export JAVA_OPTS="-Dspring.profiles.active=local" .../jboss-eap-6.2/bin/standalone.sh 

Si vous utilisez Tomcat , n’oubliez pas de définir -Dspring.profiles.active = local

Comme je le vois, le paramètre server.port sera ignoré lorsque vous exécuterez un conteneur.

J’utilise Spring Boot 1.3.1 et JBoss EAP 6.4. Et j’ai trouvé que, dans votre projet, vous pouvez append à src / main / resources / application.properties cette ligne:

 server.servlet-path=/* 

De plus, si vous lancez ceci avec Eclipse, assurez-vous de nettoyer votre projet … J’ai perdu beaucoup de temps à cause de cela seul.

Selon les réponses fournies par Michael R et István Pató, le mappage de servlet dans JBoss doit être “/ *” et non “/”. Cependant, les autres solutions provoquent l’instanciation de deux objects annotés @Component. Ce qui suit résout la double initialisation en appelant d’abord super.onStartup , puis en ajoutant un autre mappage pour le servlet de répartiteur:

 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("/*"); } } 

J’utilise JBoss EAP 6.4. Je passais dans le fil.

J’aimerais append cela après avoir changé le mappage pour dispatchServlet de “/” à “/ *”. La JSP de votre projet risque de ne pas être traitée correctement. Je soupçonne que, puisque “/ *” a plus de priorité que “/*.jsp”, JSPServlet risque donc de ne pas recevoir de demande de traitement du JSP, qui ne sera pas traité correctement. Dans mon cas, la source de JSP arrive sur le navigateur sous forme de texte.

J’ai résolu ce problème en définissant JSP comme servlet dans le fichier web.xml, comme indiqué ci-dessous. Après ça a bien fonctionné pour moi 🙂

  index.jsp   LoginServlet /login.jsp   IndexServlet /index.jsp    LoginServlet /login.jsp   IndexServlet /index.jsp  

J’ai passé beaucoup de temps là-dessus, cela pourrait aider quelqu’un :))

Eh bien, pour quelqu’un d’autre confronté à ce problème, cela fonctionne parfaitement sur le nouveau Wildfly. Si vous n’avez pas de conteneur spécifique pour exécuter votre application, vous pouvez choisir entre jboss 7 et wildfly ET vous souhaitez exécuter spring java config, essayez-le sur wildfly!

J’ai eu des problèmes similaires avec JBoss 6.4.0. et Spring Boot 1.3 en combinaison avec Tiles 3. Après l’installation du correctif Jboss jboss-eap-6.4.6-patch.zip, les problèmes ont été résolus.

Après le correctif, je n’ai pas eu besoin d’utiliser le paramètre server.servlet-path = / *