简体   繁体   English

助焊剂返回空对象

[英]Flux returns empty objects

I am starting with Flux today for it is pretty powerful. 我今天从Flux开始,因为它功能强大。 Now I have set up a whole simple Spring boot 2 project to work with this but the returned objects are empty. 现在,我已经建立了一个完整的Spring Boot 2项目来处理此问题,但是返回的对象为空。

I started a very simple Spring Boot project with some dependencies: 我开始了一个非常简单的带有某些依赖项的Spring Boot项目:

  • Reactive Web (embedded Netty + Spring WebFlux) 响应式Web(嵌入式Netty + Spring WebFlux)
  • Reactive MongoDB (Spring Data MongoDB) 反应性MongoDB(Spring Data MongoDB)
  • Thymeleaf template engine Thymeleaf模板引擎
  • Lombok (to simplify writing POJOs) 龙目岛(简化编写POJO)

And added some code: 并添加了一些代码:

controller: 控制器:

@RestController
public class ChapterController {

    @Autowired
    private ChapterRepository repository;

    @GetMapping("/chapters")
    public Flux<Chapter> listing() {
        return repository.findAll();
    }
}

Repository: 库:

public interface ChapterRepository extends ReactiveCrudRepository<Chapter, String> {}

Configuration: (to load some data in the embeded Mongodb) @Configuration public class LoadDatabase { 配置:(以在嵌入的Mongodb中加载一些数据) @Configuration公共类LoadDatabase {

    @Bean
    CommandLineRunner init(ChapterRepository repository){
        return args -> {
            Flux.just(
                new Chapter("The life of Batman"),
                new Chapter("Batmans most glorious' code"),
                new Chapter("The hero we needed but didn't deserve, Batman."))
                    .flatMap(repository::save)
                    .subscribe(System.out::println);
        };
    }
}

Data class: 资料类别:

@Data
@Document
public class Chapter {

    @Id
    private String id;
    private String name;

    public Chapter(String name) {
        this.name = name;
    }
}

Ok, now when I start the application and access the endpoint: http://localhost:8080/chapters it returns this: 好的,现在当我启动应用程序并访问端点: http:// localhost:8080 / chapters时,它将返回以下内容:

[
   {},
   {},
   {}
]

It is displaying the same amount of objects that I created in the LoadDatabase class. 它显示与在LoadDatabase类中创建的对象数量相同的对象。 When I change the amount of objects created, it shows that amount on the endpoint. 当我更改创建的对象数量时,它将在端点上显示该数量。

I don't know what I did wrong, I tried debugging the flux object returned. 我不知道自己做错了什么,我尝试调试返回的助焊剂对象。 But I can't make anything out of it. 但是我不能从中得到任何东西。

I hope someone can spot my mistake! 我希望有人能发现我的错误!

You are getting empty objects because the data is not saved and something went wrong. 您正在获取空对象,因为未保存数据并且出了点问题。

You are using @Data lombok annotation which is like having implicit @Getter, @Setter, @ToString, @EqualsAndHashCode and @RequiredArgsConstructor annotations on the class (except that no constructor will be generated if any explicitly written constructor exists). 您正在使用@Data lombok注释,就像在类上具有隐式@ Getter,@ Setter,@ ToString,@ EqualsAndHashCode和@RequiredArgsConstructor注释一样(除非存在任何显式编写的构造函数,否则不会生成任何构造函数)。 but sometimes it doesn't work if not configured in IDE correctly so try once with manual getters and setters for the properties. 但有时,如果未在IDE中正确配置,它将无法正常工作,因此请尝试使用手动getter和setter方法设置属性。

If the manual getters/setters work then try below to troubleshoot for lombok. 如果手动吸气器/安装器正常工作,请尝试以下操作以对Lombok进行故障排除。

Ensure that your IDE is aware of lombok. 确保您的IDE知道lombok。

IntelliJ : Lombok added but getters and setters not recognized in Intellij IDEA IntelliJ: 添加了Lombok,但是在Intellij IDEA中无法识别吸气剂和吸气剂

Eclipse : Lombok is not generating getter and setter Eclipse: Lombok不会生成getter和setter

If the problem still exists then follow this similar thread's one of the comments here 如果问题仍然存在,那么按照此类似线程的一个评论点击这里

如何检查助焊剂<object>是空的还是不空的?<div id="text_translate"><p> 我有一个api供kube.netes调用检测服务是否可用。在api中,先调用一个接口获取其他服务的host,接口返回一个Flux,如果结果为空api返回SERVICE_UNAVAILABLE其他返回ok .我目前的代码如下:</p><pre class="lang-java prettyprint-override"> @GetMapping(value = "/gateway/readiness") public Mono<Long> readiness(ServerHttpResponse response) { Flux<Map<String, List<String>>> hosts = hostProvider.getHosts(); List<String> hostProviders = new ArrayList<>(); // the below code has a warning: Calling subscribe in a non-blocking scope hosts.subscribe(new Consumer<Map<String, List<String>>>() { @Override public void accept(Map<String, List<String>> stringListMap) { hostProviders.addAll(stringListMap.keySet()); } }); if (hostProviders.isEmpty()) { response.setStatusCode(HttpStatus.SERVICE_UNAVAILABLE); } return routeLocator.getRoutes().count(); }</pre><p> 有优雅做到这一点?</p></div></object> - How to check Flux<Object> is empty or not?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 spring web 中压缩两个独立的通量返回空 - Zipping two independent Flux in spring web Flux Returns Empty 助焊剂:如何测试空的助焊剂 - Flux:How to test empty Flux JSONProvider返回空对象,例如String - JSONProvider returns empty objects such as String eventsource-android 从 Flux 服务器端点返回空数据字符串 - eventsource-android returns empty data String from Flux server endpoint 如何检查助焊剂<object>是空的还是不空的?<div id="text_translate"><p> 我有一个api供kube.netes调用检测服务是否可用。在api中,先调用一个接口获取其他服务的host,接口返回一个Flux,如果结果为空api返回SERVICE_UNAVAILABLE其他返回ok .我目前的代码如下:</p><pre class="lang-java prettyprint-override"> @GetMapping(value = "/gateway/readiness") public Mono<Long> readiness(ServerHttpResponse response) { Flux<Map<String, List<String>>> hosts = hostProvider.getHosts(); List<String> hostProviders = new ArrayList<>(); // the below code has a warning: Calling subscribe in a non-blocking scope hosts.subscribe(new Consumer<Map<String, List<String>>>() { @Override public void accept(Map<String, List<String>> stringListMap) { hostProviders.addAll(stringListMap.keySet()); } }); if (hostProviders.isEmpty()) { response.setStatusCode(HttpStatus.SERVICE_UNAVAILABLE); } return routeLocator.getRoutes().count(); }</pre><p> 有优雅做到这一点?</p></div></object> - How to check Flux<Object> is empty or not? 在一个通量<Flux<T> &gt;,如果是内Flux,如何完成外Flux<T> 最终是空的 - In a Flux<Flux<T>>, how to complete the outer Flux if the inner Flux<T> is eventually empty 通量列表<objects>无阻塞</objects> - Flux to List<Objects> without blocking Reactor:如何将实体的通量转换为 DTO 对象的通量 - Reactor : How to convert a Flux of entities into a Flux of DTO objects Gson调用在多个类上返回空对象 - Gson calls returns empty objects over multiple classes 我的 jax-ws 网络服务客户端只返回空对象 - My jax-ws webservice client returns only empty objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM