小编K2m*_*J33的帖子

Intellij IDEA 2020.3.3 中 Switch 的 Java 16 模式匹配问题(预期表达)

正如我们所知,Java 16 带来了新特性,如记录、密封接口和类,以及模式匹配。

今天我想在我的培训项目中使用它们。但是,我遇到了一个问题,也许我不明白什么。

因此,在代表我的 Intellij Idea 项目的给定代码中,情况看起来像:我有包:客户端和订单客户端类是一个密封接口,具有三个实现:Regular、Vip 和 SuperVip:

package com.example.records.domain.client;
public sealed interface Client permits Client.Regular, Client.SuperVip, Client.Vip {
        
            Limit currentLimit();
        
            record Regular() implements Client {
        
                @Override
                public Client.Limit currentLimit() {
                    return Client.Limit.of((short) 10);
                }
            }
        
        
            record Vip() implements Client {
        
                @Override
                public Client.Limit currentLimit() {
                    return Client.Limit.of((short) 20);
                }
            }
        
            record SuperVip() implements Client {
        
                @Override
                public Client.Limit currentLimit() {
                    return Client.Limit.of((short) 100);
                }
            }
        
        
            record Limit(short value) {
                public Limit {
                    if (value < 0) throw …
Run Code Online (Sandbox Code Playgroud)

java intellij-idea java-16 java-sealed-type

6
推荐指数
2
解决办法
701
查看次数

使用Project Reactor中的ExchangeFunction从ClientRequest下载文件并保存文件

在Project Reactor中完成文件下载后,我无法正确保存文件。

class HttpImageClientDownloader implements ImageClientDownloader {

    private final ExchangeFunction exchangeFunction;

    HttpImageClientDownloader() {
        this.exchangeFunction = ExchangeFunctions.create(new ReactorClientHttpConnector());
    }

    @Override
    public Mono<File> downloadImage(String url, Path destination) {

        ClientRequest clientRequest = ClientRequest.create(HttpMethod.GET, URI.create(url)).build();
        return exchangeFunction.exchange(clientRequest)
                .map(clientResponse -> clientResponse.body(BodyExtractors.toDataBuffers()))
                //.flatMapMany(clientResponse -> clientResponse.body(BodyExtractors.toDataBuffers()))
                .flatMap(dataBuffer -> {

                    AsynchronousFileChannel fileChannel = createFile(destination);
                    return DataBufferUtils
                            .write(dataBuffer, fileChannel, 0)
                            .publishOn(Schedulers.elastic())
                            .doOnNext(DataBufferUtils::release)
                            .then(Mono.just(destination.toFile()));


                });

    }

    private AsynchronousFileChannel createFile(Path path) {
        try {
            return AsynchronousFileChannel.open(path, StandardOpenOption.CREATE);
        } catch (Exception e) {
            throw new ImageDownloadException("Error while creating file: " + path, …
Run Code Online (Sandbox Code Playgroud)

reactive-programming spring-boot project-reactor spring-webflux

3
推荐指数
1
解决办法
1269
查看次数

Groovy在java注释中插入多行字符串

是否可以编译带有常量 @Sql 注释的常规代码?

下面的代码是简单的测试,用 Spock 编写。

@Sql(statements = ["""
         INSERT INTO pracownik ($Fields.KOMPETENCJA_ID, nr_ewid) 
                                values (1, 'A');
         INSERT INTO typ_zadania (id, kod) values (1, 'KOD');
"""]
)
def "should add new qualification"() { 
  //test code omitted
}
Run Code Online (Sandbox Code Playgroud)

当我想运行测试方法时,我在编译时收到错误:

Groovyc:预期 ' INSERT INTO pracownik ($Fields.KOMPETENCJA_ID, nr_ewid) 值 (1, 'A'); INSERT INTOtyp_zadania(id,kod)值(1,'KOD'); 成为 @org.springframework.test.context.jdbc.Sql 中 java.lang.String 类型的内联常量`

我认为带美元符号的多行字符串被评估为 GString 对象,但语句字段是字符串数组的类型。

我可以在groovy代码中使用多行字符串中的java注释常量吗?

java groovy jvm compiler-errors spock

2
推荐指数
1
解决办法
945
查看次数