小编du-*_*-it的帖子

是否可以将正则表达式与 OpenApi / Swagger @RequestParam 一起使用?

我想为要使用 Swagger 记录的请求参数定义正则表达式。事实上我想定义文件名的格式。在Spring(引导)中使用注释可以吗?
这里:

   @Bean
    default OpenAPI customOpenApi() {

        return new OpenAPI()
            .info(new Info()
                .title("")
                .version("1.0")
                .description(""))
            .components(new Components()
                    .addParameters(
                            "api-version", new io.swagger.v3.oas.models.parameters.Parameter()
                            .name("api-version")
                            .required(Boolean.TRUE)
                            .allowEmptyValue(Boolean.FALSE)
                            .description("")
                            .in("header")
                            .schema(new io.swagger.v3.oas.models.media.Schema<String>().type("string").example("1.0.0"))
                    )
                    .addParameters(
                            "file-name", new io.swagger.v3.oas.models.parameters.Parameter()
                            .name("file-name")
                            .required(Boolean.TRUE)
                            .allowEmptyValue(Boolean.TRUE)
                            .description("Filename of the file to be imported in the following format: " +
                        "<ul>" +
                        " <li><b>yyyyMMdd_DocumentType_senderID_rexeiverID_xxx_version.xml</b></li>" +
                        "</ul>")
                            .in("header")
                            .schema(new io.swagger.v3.oas.models.media.Schema<Integer>().type("string").example("20210228_A14_123456789_987654321_xxx_123.xml"))
                    )
            );
    }
Run Code Online (Sandbox Code Playgroud)

或在这里:

    ResponseEntity<ResponseBody> postData(
        @RequestHeader HttpHeaders headers,
        @RequestAttribute(name = REQUST_ID, required = false) String requestId,
        @Parameter(name = API_VERSION_HEADER_PARAMETER) …
Run Code Online (Sandbox Code Playgroud)

regex swagger spring-boot openapi

4
推荐指数
1
解决办法
7485
查看次数

如何使用 MapStruct 将日期字符串转换为另一种格式?

我正在使用 MapStruct 将值从源映射到目标类。两个类都应具有日期属性,但日期格式不同。如何在使用 MapStruct 映射属性时转换日期格式?

源类的日期格式:2018-05-18T18:43:33.623+0200

目标班级日期格式:2018-05-18

format date mapstruct

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

将Java Generics与HashMap一起使用

我有一个B和C类,它们都扩展到A类:

public class B extends A {...}
public class C extends A {...}
Run Code Online (Sandbox Code Playgroud)

如何以这种方式将Java泛型与HashMap一起使用?

B b = new B();
C c = new C();

Map<String, ? extends A> map = new HashMap<String, A>();

map.put("B", b);
map.put("C", c);
Run Code Online (Sandbox Code Playgroud)

Eclipse始终显示错误:

方法put(String,capture#1-of?extends A)在Map类型中不适用于参数(String,B)

方法put(String,capture#1-of?extends A)在Map类型中不适用于参数(String,C)

java generics

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

Java 8 stream sum and count at once

How can I use Java 8 streams to calculate an average of the stream elements? Because I cannot use a stream twice I have to do it in one iteration. The stream contains custom beans holding a float value. The number of elements in the stream may vary. Hence, I want to sum up the float value of all elements in the stream and divide it by the number of elements in the stream.

Update: Finally I need the sum …

java average java-stream

0
推荐指数
1
解决办法
118
查看次数

为什么 Testcontainers 不使用 application.properties / application.yaml?

在我的 Spring Boot 2.4.3 应用程序中,我使用 Testcontainers 并按照互联网上的说明进行操作。我有一个application.yaml

spring:
  datasource:
    url: jdbc:tc:postgresql:13.2:///testdb?TC_INITSCRIPT=tc_initscript_postgresql.sql
    username: duke
    password: s3crEt
  jpa:
    database-platform: org.hibernate.dialect.PostgreSQL95Dialect
    hibernate:
      ddl-auto: create-drop
Run Code Online (Sandbox Code Playgroud)

但是当我调试应用程序时,容器始终将“test”作为 URL、用户名和密码的值。

这是我的测试课:

@ActiveProfiles("test")
@Testcontainers
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public abstract class AbstractApplicationIT {

    final static DockerImageName POSTGRES_IMAGE = DockerImageName.parse("postgres:13.2-alpine");

        @Container
    //    public static GenericContainer postgreSQLContainer = new GenericContainer(POSTGRES_IMAGE)
        public static PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>(POSTGRES_IMAGE)
    //            .withInitScript("tc_initscript_postgresql.sql")
    //            .withPassword("password")
    //            .withUsername("username")
    //            .withDatabaseName("test")
    //            .withInitScript("tc_initscript_postgresql.sql")
                ;
    
    //    @DynamicPropertySource
    //    static void postgresqlProperties(DynamicPropertyRegistry registry) {
    //        registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl); …
Run Code Online (Sandbox Code Playgroud)

spring-boot application.properties testcontainers

0
推荐指数
1
解决办法
3866
查看次数

ArrayList 上的 isEmpty() 结果为 false,尽管其大小为 0

在测试中,我向另一个类注入了一个模拟,该类似乎工作正常。但是,当我检查 ArrayList 是否为空时,尽管其长度/大小为 0,结果却为 false。这种情况怎么会发生以及如何解决这个问题?

@Slf4j
@Configuration
@RequiredArgsConstructor
@Setter(onMethod_ = @SuppressFBWarnings({"EI_EXPOSE_REP2", "EI_EXPOSE_REP"}))
@Getter(onMethod_ = @SuppressFBWarnings({"EI_EXPOSE_REP2", "EI_EXPOSE_REP"}))
@EnableConfigurationProperties(MyProperties.class)
public class MyConfig {

    private final MyProperties myProperties;

    private final GenericApplicationContext applicationContext;

    @PostConstruct
    void init() {
        Objects.requireNonNull(myProperties, "myProperties needs not to be null.");
        if (/*myProperties.getApps().size() == 0 || */myProperties.getApps().isEmpty()) {
            log.info("bla bla bla");
        } else {
            ...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的测试课:

@ExtendWith(MockitoExtension.class)
class MyConfigTest {

    @Mock
    MyProperties myPropertiesMock;

    @InjectMocks
    MyConfig myConfig;

    ApplicationContextRunner contextRunner;

    @Test
    void should_check_for_empty_apps() {
        contextRunner = new ApplicationContextRunner()
            .withPropertyValues("foobar.apps[0].name=", "foobar.apps[0].baseUrl=", …
Run Code Online (Sandbox Code Playgroud)

java arraylist is-empty

0
推荐指数
1
解决办法
814
查看次数