无法加载应用程序上下文(Spring Boot)



我正在尝试按照 Spring 网站上的示例测试我的应用程序。

这些是我的依赖项:

dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile("org.springframework.boot:spring-boot-starter-security")
compile('org.springframework.boot:spring-boot-starter-web')
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.thymeleaf.extras:thymeleaf-extras-springsecurity4")
runtime('org.springframework.boot:spring-boot-devtools')
runtime('com.h2database:h2')
runtime('mysql:mysql-connector-java')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
}

这是测试类:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ProductControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testHomePage() throws Exception {
this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk());
}
}

我收到错误:

org.springframework.beans.factory.BeanCreationException: 创建在类路径资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class] 中定义名称为"entityManagerFactory"的 bean 时出错:初始化方法的调用失败;嵌套异常是 java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

java.lang.IllegalStateException: 无法加载 ApplicationContext

知道是什么原因造成的吗?我没有任何特定的配置文件,只有安全配置和网络配置。

网络配置:

public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/images/**", "/css/**", "/fragments/**")
.addResourceLocations("classpath:/static/images/", "classpath:/static/css/", "classpath:/fragments/");
}
}

输出中的以下行:

java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

告诉我们找不到类javax.xml.bind.JAXBException

如@Antot所述,将以下依赖项添加到您的 graddle 应该可以解决问题:

compile('javax.xml.bind:jaxb-api:2.3.0')

对于使用 Maven 的用户,请使用以下命令:

<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>

最新更新