8
respostas

Erro ao subir serviço - Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 2022-04-26 16:23:30.349 ERROR 18268 --- [ main] o.s.boot.SpringApplication : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1631) ~[spring-beans-4.3.19.RELEASE.jar:4.3.19.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.19.RELEASE.jar:4.3.19.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481) ~[spring-beans-4.3.19.RELEASE.jar:4.3.19.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312) ~[spring-beans-4.3.19.RELEASE.jar:4.3.19.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.19.RELEASE.jar:4.3.19.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308) ~[spring-beans-4.3.19.RELEASE.jar:4.3.19.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.19.RELEASE.jar:4.3.19.RELEASE] at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1080) ~[spring-context-4.3.19.RELEASE.jar:4.3.19.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) ~[spring-context-4.3.19.RELEASE.jar:4.3.19.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.19.RELEASE.jar:4.3.19.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.16.RELEASE.jar:1.5.16.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) ~[spring-boot-1.5.16.RELEASE.jar:1.5.16.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) ~[spring-boot-1.5.16.RELEASE.jar:1.5.16.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) ~[spring-boot-1.5.16.RELEASE.jar:1.5.16.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) ~[spring-boot-1.5.16.RELEASE.jar:1.5.16.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) ~[spring-boot-1.5.16.RELEASE.jar:1.5.16.RELEASE] at br.com.alura.forum.ForumApplication.main(ForumApplication.java:10) ~[classes/:na] Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException at org.hibernate.boot.spi.XmlMappingBinderAccess.(XmlMappingBinderAccess.java:43) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.boot.MetadataSources.(MetadataSources.java:87) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.(EntityManagerFactoryBuilderImpl.java:179) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.(EntityManagerFactoryBuilderImpl.java:149) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final] at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:54) ~[spring-orm-4.3.19.RELEASE.jar:4.3.19.RELEASE] at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:360) ~[spring-orm-4.3.19.RELEASE.jar:4.3.19.RELEASE] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:384) ~[spring-orm-4.3.19.RELEASE.jar:4.3.19.RELEASE] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:371) ~[spring-orm-4.3.19.RELEASE.jar:4.3.19.RELEASE] at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:336) ~[spring-orm-4.3.19.RELEASE.jar:4.3.19.RELEASE] at or

Log do erro que ocorre ao tentar executar o projeto.

8 respostas

Boa tarde Fernando.

Aparentemente está com problema na JPA.

Você poderia mandar como está seu arquivo POM.xml, estrutura do projeto e de configuração do banco de dados.

Pom:

4.0.0 org.springframework.boot spring-boot-starter-parent 1.5.16.RELEASE br.com.alura forum 0.0.1-SNAPSHOT forum Demo project for Spring Boot <java.version>11</java.version> org.springframework.boot spring-boot-starter-web

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.6.7</version>
        </plugin>
    </plugins>
</build>

Estrutura do projeto: Insira aqui a descrição dessa imagem para ajudar na acessibilidade

Configuração do Banco de Dados:

datasource

spring.datasource.driverClassName=org.h2.Driver spring.datasource.url=jdbc:h2:mem:alura-forum spring.datasource.username=sa spring.datasource.password=

jpa

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.jpa.hibernate.ddl-auto=update

Nova propriedade a partir da versao 2.5 do Spring Boot:

spring.jpa.defer-datasource-initialization=true

h2

spring.h2.console.enabled=true spring.h2.console.path=/h2-console

Adicionei as seguintes dependências:

<dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.0</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>javax.activation-api</artifactId>
            <version>1.2.0</version>
        </dependency>

Agora o serviço subiu, porém não está criando as tabelas.

Como está o arquivo data.sql?

Copie o log de inicialização da aplicação aqui também.

Arquivo data.sql:

INSERT INTO USUARIO(nome, email, senha) VALUES('Aluno', 'aluno@email.com', '123456');

INSERT INTO CURSO(nome, categoria) VALUES('Spring Boot', 'Programação'); INSERT INTO CURSO(nome, categoria) VALUES('HTML 5', 'Front-end');

INSERT INTO TOPICO(titulo, mensagem, data_criacao, status, autor_id, curso_id) VALUES('Dúvida', 'Erro ao criar projeto', '2019-05-05 18:00:00', 'NAO_RESPONDIDO', 1, 1); INSERT INTO TOPICO(titulo, mensagem, data_criacao, status, autor_id, curso_id) VALUES('Dúvida 2', 'Projeto não compila', '2019-05-05 19:00:00', 'NAO_RESPONDIDO', 1, 1); INSERT INTO TOPICO(titulo, mensagem, data_criacao, status, autor_id, curso_id) VALUES('Dúvida 3', 'Tag HTML', '2019-05-05 20:00:00', 'NAO_RESPONDIDO', 1, 2);

Log:

WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (file:/C:/Users/elton.silva/.m2/repository/org/springframework/spring-core/4.3.19.RELEASE/spring-core-4.3.19.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$1 WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release 2022-04-27 08:17:30.427 INFO 19700 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$fda13305] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2022-04-27 08:17:31.413 INFO 19700 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2022-04-27 08:17:31.449 INFO 19700 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-04-27 08:17:31.450 INFO 19700 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.34 2022-04-27 08:17:31.583 INFO 19700 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-04-27 08:17:31.583 INFO 19700 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2589 ms 2022-04-27 08:17:31.748 INFO 19700 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2022-04-27 08:17:31.750 INFO 19700 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'webServlet' to [/h2-console/] 2022-04-27 08:17:31.753 INFO 19700 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/] 2022-04-27 08:17:31.754 INFO 19700 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/] 2022-04-27 08:17:31.754 INFO 19700 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/] 2022-04-27 08:17:31.754 INFO 19700 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2022-04-27 08:17:32.299 INFO 19700 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default' 2022-04-27 08:17:32.321 INFO 19700 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [ name: default ...] 2022-04-27 08:17:32.410 INFO 19700 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.0.12.Final} 2022-04-27 08:17:32.411 INFO 19700 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found 2022-04-27 08:17:32.413 INFO 19700 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist 2022-04-27 08:17:32.462 INFO 19700 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 2022-04-27 08:17:32.594 INFO 19700 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect 2022-04-27 08:17:33.066 INFO 19700 --- [ main] org.hibernate.tuple.PojoInstantiator : HHH000182: No default (no-argument) constructor for class: br.com.alura.forum.modelo.Curso (class must be instantiated by Interceptor) 2022-04-27 08:17:33.099 INFO 19700 --- [ main] org.hibernate.tuple.PojoInstantiator : HHH000182: No default (no-argument) constructor for class: br.com.alura.forum.modelo.Topico (class must be instantiated by Interceptor) 2022-04-27 08:17:33.209 INFO 19700 --- [ main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000228: Running hbm2ddl schema update 2022-04-27 08:17:33.251 INFO 19700 --- [ main] rmationExtractorJdbcDatabaseMetaDataImpl : HHH000262: Table not found: curso 2022-04-27 08:17:33.261 INFO 19700 --- [ main] rmationExtractorJdbcDatabaseMetaDataImpl : HHH000262: Table not found: curso 2022-04-27 08:17:33.282 INFO 19700 --- [ main] rmationExtractorJdbcDatabaseMetaDataImpl : HHH000262: Table not found: resposta

Fernando,

Pelo seu arquivo POM.xml, você não está usando uma versão superior a 2.5 do SpringBoot.

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.16.RELEASE</version>
    </parent>

Você pode tentar o seguinte teste: Abra o arquivo "application.properties" e remova a linha:

#Nova propriedade a partir da versao 2.5 do Spring Boot:
spring.jpa.defer-datasource-initialization=true

E tente executar a aplicação.

Fiz um teste aqui com a versão 2.1.4.RELEASE do SpringBoot e funcionou sem o código "spring.jpa.defer-datasource-initialization=true".

Quando fiz esse curso, eu utilizei a versão 2.6.1 do SpringBoot e por esse motivo tive que usar essa propriedade, mas enquanto você manter a versão do SpringBoot menor que a 2.5 não é necessário.

Deu esse erro no log: HHH000182: No default (no-argument) constructor for class: br.com.alura.forum.modelo.Topico

Você precisa criar um construtor default nas entidades Curso e Topico.