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

SpringBoot 2无法bing ConfigurationProperties

景昊焜
2023-03-14

我的财产包括:

@Component
@Validated
@ConfigurationProperties("kafka")
public class KafkaProperties {

    @NotEmpty
    String broker;

    @NotEmpty
    String groupId;

    @NotEmpty
    String resetOffset = "latest";

    @NotEmpty
    String topic;

}

我的申请。属性,如:

kafka.broker=localhost:9092
kafka.groupid=pdp-group
kafka.offset-reset=latest
kafka.topic=pdp-product-fragment

测试是:

@ExtendWith(SpringExtension.class)
@Import({ReactiveKafkaConsumerTest.TestConfig.class})
@PropertySource("classpath:application.properties")
class ReactiveKafkaConsumerTest {

    @RegisterExtension
    static final SharedKafkaTestResource sharedKafkaTestResource = new SharedKafkaTestResource();

    static class TestConfig extends KafkaConfig {

        Consumer<ReceiverRecord<String, String>> mockConsumer = mock(Consumer.class);

        @Override
        List<String> topics() {
            String topicName = Integer.toHexString(Math.abs(new Random().nextInt()));
            var kafkaTestUtils = sharedKafkaTestResource.getKafkaTestUtils();
            kafkaTestUtils.createTopic(topicName, 4, (short) 1);
            return asList(topicName);
        }

        @Override
        public Map<String, Object> consumerProps() {
            Map<String, Object> props = super.consumerProps();
            props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, sharedKafkaTestResource.getKafkaConnectString());
            props.put(ConsumerConfig.GROUP_ID_CONFIG, "group");
            props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
            return props;
        }

        @Override
        public Supplier<Consumer<ReceiverRecord<String, String>>> consumerSupplier() {
            return () -> mockConsumer;
        }


    }

    @Autowired
    List<String> topics;

    @Autowired
    TestConfig testConfig;

    @Test
    @DirtiesContext
    public void testReceiveMessages() {
        var kafkaTestUtils = sharedKafkaTestResource.getKafkaTestUtils();

        IntStream.range(0, 4)
                .forEach(i -> kafkaTestUtils.produceRecords(4, topics.get(0), i));


        ArgumentCaptor<ReceiverRecord<String, String>> captor = ArgumentCaptor.forClass(ReceiverRecord.class);
        verify(testConfig.mockConsumer, timeout(10000).times(16))
                .accept(captor.capture());

        var recordsPerPartition = captor.getAllValues().stream()
                .collect(Collectors.groupingBy(r -> r.partition()));

        assertThat(recordsPerPartition.keySet(), containsInAnyOrder(0, 1, 2, 3));
        assertEquals(16, recordsPerPartition.values().stream().mapToInt(l -> l.size()).sum());
    }

}

故障原因:

Caused by: org.springframework.boot.context.properties.bind.validation.BindValidationException: Binding validation errors on kafka
   - Field error in object 'kafka' on field 'broker': rejected value [null]; codes [NotEmpty.kafka.broker,NotEmpty.broker,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [kafka.broker,broker]; arguments []; default message [broker]]; default message [must not be empty]
   - Field error in object 'kafka' on field 'topic': rejected value [null]; codes [NotEmpty.kafka.topic,NotEmpty.topic,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [kafka.topic,topic]; arguments []; default message [topic]]; default message [must not be empty]
   - Field error in object 'kafka' on field 'groupId': rejected value [null]; codes [NotEmpty.kafka.groupId,NotEmpty.groupId,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [kafka.groupId,groupId]; arguments []; default message [groupId]]; default message [must not be empty]

我尝试过很多事情,比如:

  • 添加SpringBootTest将PropertySource替换为
  • TestPropertySource测试属性源

Spring开机版本为:2.2.2。释放

有人有主意吗?

共有1个答案

孙梓
2023-03-14

将属性或yaml文件绑定到POJO是Spring Boot功能加载yaml,所以如果您想在测试期间使用它,您需要使用@SpringBootTest加载测试应用程序上下文。我还建议将@Configuration与@Configurationproperties一起使用。请参阅此处了解更多信息

注意:如果我们不在POJO中使用@Configuration,那么我们需要在Spring主应用程序类中添加@EnableConfigurationProperties(ConfigProperties.class)来将属性绑定到POJO中:

 类似资料:
  • 我正在尝试使用 https://github.com/spring-projects/spring-security-oauth2-boot 使用本教程:https://docs.spring.io/spring-security-oauth2-boot/docs/current-SNAPSHOT/reference/htmlsingle/ SpringBoot应用程序 服务器初始化器 用户详细信

  • 本文向大家介绍springboot2版本无法加载静态资源问题解决,包括了springboot2版本无法加载静态资源问题解决的使用技巧和注意事项,需要的朋友参考一下 前言 在学习springboot的过程中,发现无法引用静态资源。我使用的是springboot2.2.1版本。 追溯源码,终于解决。并记录下解决思路。 默认加载路径 首先得知道springboot默认加载得资源路径是什么。 首先我们看W

  • 本文向大家介绍SpringBoot2使用WebFlux函数式编程的方法,包括了SpringBoot2使用WebFlux函数式编程的方法的使用技巧和注意事项,需要的朋友参考一下 本文只是简单使用SpringBoot2使用WebFlux的函数式编程简单使用,后续会继续写关于Webflux相关的文章。 最近一直在研究WebFlux,后续会陆续出一些相关的文章。 首先看一下Srping官网上的一张图,对比

  • 本文向大家介绍SpringBoot2.X Devtools热部署实现解析,包括了SpringBoot2.X Devtools热部署实现解析的使用技巧和注意事项,需要的朋友参考一下 pom文件引入所需jar包 springboot版本采用2.3.1 启动设置成热部署 修改代码后点击刷新即可 控制台重新打印SpringBoot图标就可以了 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多

  • 我试着用Springboot2做一些测试,特别是用Reactor(Flux/Mono)。我面临一个问题。 当我尝试使用一个存储库方法做检索我所有的人我使用该方法:@Tailable Flux findRetTailableCursorBy(); 但就我而言,我收到了这个错误 当我将我的收藏设置为“封顶”时,它就起作用了 但出于很多原因,我不会使用封顶系列 无法删除文档(必须删除收藏) 无法通过编程

  • 本文向大家介绍基于springboot2集成jpa,创建dao的案例,包括了基于springboot2集成jpa,创建dao的案例的使用技巧和注意事项,需要的朋友参考一下 springboot中集成jpa需要再pom文件中添加jpa的jar包,使用springboot的话iju不用自己规定版本号了,自动管理依赖版本即可。 然后我们再添加hibernate和oracle的jar包,同样自动管理版本。

  • 本文向大家介绍SpringBoot2 JPA解决懒加载异常的问题,包括了SpringBoot2 JPA解决懒加载异常的问题的使用技巧和注意事项,需要的朋友参考一下 jpa解决懒加载异常 在我上一遍文章上进行行修改,SpringBoot2 实现JPA分页和排序分页 实体类上改: 控制器验证 我大概实现了一下,具体的如果大佬找到更好的方法或者发现我的方法是错的,希望各位大佬提醒一下!感谢! 补充:Sp

  • 本文向大家介绍SpringBoot2使用Jetty容器操作(替换默认Tomcat),包括了SpringBoot2使用Jetty容器操作(替换默认Tomcat)的使用技巧和注意事项,需要的朋友参考一下 Jetty和tomcat的比较 Tomcat和Jetty都是一种Servlet引擎,他们都支持标准的servlet规范和JavaEE的规范。 架构比较 Jetty的架构比Tomcat的更为简单 Jet