当前位置: 首页 > 知识库问答 >
问题:

由于TestRestTemplate和apache SSL,Spring Boot集成无法启动

公西天逸
2023-03-14

我使用的是Spring Boot1.4、Spock和Spring-boot-starter-security。这是我的相关年级档案:

springBootVersion = '1.4.0.BUILD-SNAPSHOT'
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-devtools')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.jasypt:jasypt-spring31:1.9.2')
compile('org.apache.httpcomponents:httpclient:4.5.2')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.spockframework:spock-core:1.1-groovy-2.4-rc-2')
testCompile('org.spockframework:spock-spring:1.1-groovy-2.4-rc-2')

我还将其添加到我的application-test.properties文件中:

server.ssl.enabled=false
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
@Configuration
public class DBConfig {

    @Autowired
    private Environment env;

    @Value("${spring.datasource.driverClassName}")
    private String databaseDriverClassName;

    @Value("${spring.datasource.url}")
    private String datasourceUrl;

    @Value("${spring.datasource.username}")
    private String databaseUsername;

    @Value("${jasypt.key}")
    private String jasyptKey;

    @Bean
    public DataSource datasource() throws IOException {

        return DataSourceBuilder
                        .create()
                        .username(databaseUsername)
                        .password(getSecurePassword())
                        .url(datasourceUrl)
                        .driverClassName(databaseDriverClassName)
                        .build();
    }

    private String getSecurePassword() throws IOException {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        encryptor.setPassword(jasyptKey);
        Properties props = new EncryptableProperties(encryptor);
        props.load(this.getClass().getClassLoader().getResourceAsStream("application-" + env.getActiveProfiles()[0] + ".properties"));
        return props.getProperty("spring.datasource.password");
    }
}

我的基测试类将启动一个嵌入式Tomcat实例并使所有endpoint可用,并且有一个获取JWT的方法:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource("classpath:application-test.properties")
class BaseSpecification extends Specification {

    @Autowired
    def TestRestTemplate restTemplate

    String loginAndGetJWT() {

        def model = new ModelMap();
        model.addAttribute("username", "ausername")
        model.addAttribute("password", "apassword");

        ResponseEntity<JwtAuthenticationResponse> response = restTemplate.getForEntity("/auth", String.class, model);
        def jwt = response.getBody().token

        return jwt;
    }
}

当我尝试运行示例测试时:

class ApplicationSpecWithoutAnnotation extends BaseSpecification {

    @Autowired
    WebApplicationContext context

    def "should boot up without errors"() {

        expect: "web application context exists"
        context != null
    }
}

我得到以下信息:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testRestTemplate': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.apache.http.conn.ssl.SSLConnectionSocketFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testRestTemplate': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/http/impl/client/HttpClients

共有1个答案

公西岳
2023-03-14

原来我所包含的QBO库有一个冒犯性的http库。我删除了对2.5库的所有引用,我的构建被修复了。不知道为什么它没有出现在我的gradle依赖树中,但当我查看导入的JAR时,我发现了它。

 类似资料:
  • 我一直在尝试启动NiFi,但每次我这样做,我都得到以下错误:

  • 我在eclipse安装中复制了一些插件(特别是Green和ObjectAid)。 我删除了它们(因为它不起作用),但现在我无法启动eclipse(即使使用)。 我得到: !session 2013-02-08 !Entry org.eclipse.osgi 4 0 201 3-02-08 16:21:17.779!Message Application error!Stack 1 java.lan

  • 我正在尝试启动一个简单的spring应用程序 我有主管道。java文件就在这里: 这是pom。xml: 最后一个错误是: 据我所知我少了一颗豆子?然而,看起来好像我有罐子春豆。

  • 在CentOS7上运行HBase 2.0.4和Hadoop 2.8.5,其中有1个主节点、4个从节点。我在HBase2.1.3上尝试过同样的设置,也出现了同样的问题。 由于Zookeeper未解析HRegionservers,HMaster无法启动,如此错误日志所示。 我的配置文件如下所示: ----hbase-site.xml--- ----区域服务器---- ----/etc/hosts---

  • Maven build成功了,但当我尝试运行它时失败了: 我有带 一切似乎都准备就绪。发生了什么? pom.xml 更新1 使用IntelliJ构建jar工件时也是如此。 更新2 好的,我设法运行了它,但现在我有: 更新3 通过添加到应用程序使其正常工作。爪哇:

  • 我们在运行命令“minikube start--drive=hyv”时遇到以下错误,该命令最初工作正常,但突然开始出错。 C:\Windows\system32 迷你库贝 v1.22.0 on 微软视窗 10 企业 10.0.19043 内部版本 19043 根据用户配置使用 hyperv 驱动程序 由于PROVIDER_HYPERV_NOT_FOUND而退出:找不到“hyperv”提供程序:C: