小编use*_*540的帖子

ReplaceAll与java8 lambda函数

给出以下变量

templateText = "Hi ${name}";
variables.put("name", "Joe");
Run Code Online (Sandbox Code Playgroud)

我想使用以下代码(不起作用)将值占位符$ {name}替换为值"Joe"

 variables.keySet().forEach(k -> templateText.replaceAll("\\${\\{"+ k +"\\}"  variables.get(k)));
Run Code Online (Sandbox Code Playgroud)

但是,如果我采用"旧式"的方式,一切都很完美:

for (Entry<String, String> entry : variables.entrySet()){
    String  regex = "\\$\\{" + entry.getKey() + "\\}";          
    templateText =  templateText.replaceAll(regex, entry.getValue());           
   }
Run Code Online (Sandbox Code Playgroud)

当然我在这里遗漏了一些东西:)

java lambda java-8

9
推荐指数
2
解决办法
6457
查看次数

spring-boot2 上的 Resilience4j - 断路器未打开

遵循入门指南(https://resilience4j.readme.io/docs/getting-started-3)和演示项目(https://github.com/resilience4j/resilience4j-spring-boot2-demo)我想自己测试它,创建一个更简单的测试应用程序。

这是代码:


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ServiceConfiguration {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

Run Code Online (Sandbox Code Playgroud)

控制器:


@RestController
public class ServiceController {

    @Autowired
    private  AlbumService albumService;


    @GetMapping("/albums")
    ResponseEntity<String> getAlbums() {
       return albumService.getAlbums();
    }

}

Run Code Online (Sandbox Code Playgroud)

和服务类:

@Slf4j
@Service
public class AlbumService {

    @Autowired
    private  RestTemplate restTemplate;

    @CircuitBreaker(name = "albumService", fallbackMethod = "getDefaultAlbumList")
    public ResponseEntity<String> getAlbums() {
        String url = MockNeat.secure().probabilites(String.class)
                .add(0.7, "https://wrong-url.com")
                .add(0.3, "https://jsonplaceholder.typicode.com/albums").val();
        return new ResponseEntity<>(restTemplate.getForObject(url, String.class), HttpStatus.OK);
    }

    private …
Run Code Online (Sandbox Code Playgroud)

java spring resilience4j

7
推荐指数
1
解决办法
2447
查看次数

使用 testcontainers 播种 mysql 数据库

我的情况非常简单:AWS 中现有的 mysql 数据库我想使用 testcontainers 进行测试。

\n\n

我遵循了官方指南(https://www.testcontainers.org/modules/databases/)及其示例(https://github.com/testcontainers/testcontainers-java/blob/master/modules/jdbc-test/ src/test/java/org/testcontainers/junit/SimpleMySQLTest.java),但是我无法加载使用 mysqldump 创建的转储。

\n\n

每次我都会收到以下错误:

\n\n
Caused by: java.sql.SQLSyntaxErrorException: Access denied; you need (at least one of) the SUPER privilege(s) for this operation\n
Run Code Online (Sandbox Code Playgroud)\n\n

根据其他用户的建议,我尝试了以下方法:

\n\n
MySQLContainer mysql = (MySQLContainer) new MySQLContainer()\n                .withInitScript("foodmart_department.sql")\n                .withUsername("root")\n                .withPassword("test")\n                .withLogConsumer(new Slf4jLogConsumer(logger));\n        mysql.start();\n
Run Code Online (Sandbox Code Playgroud)\n\n

但它挂起几分钟并因以下错误而终止:

\n\n
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".\nSLF4J: Defaulting to no-operation (NOP) logger implementation\nSLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.\n\n        \xe2\x84\xb9\xef\xb8\x8e Checking the system...\n        \xe2\x9c\x94 Docker version should be at …
Run Code Online (Sandbox Code Playgroud)

java mysql testcontainers

5
推荐指数
1
解决办法
5677
查看次数

概括一种方法

我有这个方法

public static Integer addCollectionElements(List<Integer> list) {
    return list.stream().reduce(0, Integer::sum).intValue();
} 
Run Code Online (Sandbox Code Playgroud)

我想推广到任何实现方法总和的类(例如Float).

是否可以通过使用Java Generics实现这一目标?

java generics java-8 java-stream

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

Cypress - 比较两个输入的相等性

使用赛普拉斯(刚刚开始),我找不到一种方法来断言两个输入框中文本的相等性,如图所示。用例是,当用户设置取件位置时,默认情况下相同的位置应出现在下车框中。

为了测试上述内容,我编写了以下代码:

  cy.get('input#dropFtsAutocomplete').should("have.value" , cy.get('input#ftsAutocomplete'));
Run Code Online (Sandbox Code Playgroud)

正确地,赛普拉斯抱怨以下错误:

错误:AssertionError: 预期 '' 具有值 { Object (chainerId, firstCall) },但值为 'Manchester Airport (MAN), Manchester, United Kingdom'

我错过了什么?

javascript testing jquery chai cypress

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

Java NIO ByteBuffer:放置和获取字符串

我有以下测试代码。我想知道如何使用 Java NIO ByteBuffer 放置和获取字符串。我在需要帮助的地方添加了两条评论。

package testPipe;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

public class TestMemBuff {

    static final String dataFile = "invoicedata";
    static final double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
    static final int[] units = { 12, 8, 13, 29, 50 };
    static final String[] descs = { "Java T-shirt", "Java Mug",
            "Duke Juggling Dolls", "Java Pin", "Java Key Chain" };

    public static Charset charset = Charset.forName("UTF-8");
    public static CharsetEncoder encoder …
Run Code Online (Sandbox Code Playgroud)

java

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

Java 8计算最小值

我正在练习Java 8.我不明白为什么这个方法总是返回0,或者更好的身份值:

public static Integer getMinAge(List<Person> peopleList) {
    return peopleList.stream().mapToInt(Person::getAge).reduce(0, Integer::min);
}
Run Code Online (Sandbox Code Playgroud)

令人惊讶的是,Integer :: max方法返回正确的值.我在这做错了什么?

java java-8

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

LocalDateTime之间的区别

java.time库中,我使用静态方法Duration.between来计算两个LocalDateTime之间的时间(以秒为单位).

一切都按预期工作,除了下面的情况,我应该看到60秒的差异,而不是1500.

在此输入图像描述 以下是重现错误的代码:

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;

class Scratch {

    public static void main(String[] args) {

        LocalDateTime endDate = LocalDateTime.now().with(DayOfWeek.SATURDAY).withHour(0).withMinute(0);
        LocalDateTime startDate = LocalDateTime.now().with(DayOfWeek.SUNDAY).withHour(1).withMinute(0);
        System.out.println(Duration.between(endDate,startDate).toMinutes());
    }
}
Run Code Online (Sandbox Code Playgroud)

我相信我在这里遗漏了一些东西.

java datetime

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