我想为要使用 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) 我正在使用 MapStruct 将值从源映射到目标类。两个类都应具有日期属性,但日期格式不同。如何在使用 MapStruct 映射属性时转换日期格式?
源类的日期格式:2018-05-18T18:43:33.623+0200
目标班级日期格式:2018-05-18
我有一个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)
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 …
在我的 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) 在测试中,我向另一个类注入了一个模拟,该类似乎工作正常。但是,当我检查 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)