在我的 Spring Boot 项目中,我有两个 DTO 正在尝试验证,LocationDto 和 BuildingDto。LocationDto 有一个 BuildingDto 类型的嵌套对象。
这些是我的 DTO:
位置Dto
public class LocationDto {
@NotNull(groups = { Existing.class })
@Null(groups = { New.class })
@Getter
@Setter
private Integer id;
@NotNull(groups = { New.class, Existing.class })
@Getter
@Setter
private String name;
@NotNull(groups = { New.class, Existing.class, LocationGroup.class })
@Getter
@Setter
private BuildingDto building;
@NotNull(groups = { Existing.class })
@Getter
@Setter
private Integer lockVersion;
}
Run Code Online (Sandbox Code Playgroud)
建筑Dto
public class BuildingDto {
@NotNull(groups = { Existing.class, LocationGroup.class })
@Null(groups = …Run Code Online (Sandbox Code Playgroud) 我试图转换以下文本输入文件:
A=groupA1
A=groupA2
A=groupA3
B=groupB1
B=groupB2
Run Code Online (Sandbox Code Playgroud)
成Map<String, List<String>>通过划分在每一行"="
到目前为止,我想要获得这种输出:
KEY: A
VALUE: A=groupA1
VALUE: A=groupA2
VALUE: A=groupA3
KEY: B
VALUE: B=groupB1
VALUE: B=groupB2
Run Code Online (Sandbox Code Playgroud)
使用这样的代码:
File reqFile = new File("test.config");
try (Stream<String> stream = Files.lines(reqFile.toPath())) {
Map<String, List<String>> conf = stream.collect(Collectors.groupingBy(s -> s.split("=")[0]));
for (Map.Entry<String, List<String>> entry: conf.entrySet()) {
System.out.println("KEY: " + entry.getKey());
for (String value : entry.getValue()) {
System.out.println("VALUE: " + value);
}
}
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
如何调整上面的lambda得到这样的东西:
KEY: A
VALUE: groupA1
VALUE: …Run Code Online (Sandbox Code Playgroud) 我是 Spring WebClient 的新手,我编写了一个通用方法,可用于在我的应用程序中使用 REST API:
public <T> List<T> get(URI url, Class<T> responseType) {
return WebClient.builder().build().get().uri(url)
.header("Authorization", "Basic " + principal)
.retrieve().bodyToFlux(responseType).collectList().block();
}
Run Code Online (Sandbox Code Playgroud)
如果消耗了rest-api返回404,我想返回并清空列表。
有人可以建议如何实现这一目标吗?
我有两个服务应该通过Kafka. 让我们调用第一个服务WriteService和第二个服务QueryService。
在WriteService端,我对生产者有以下配置。
@Configuration
public class KafkaProducerConfiguration {
@Value("${spring.kafka.bootstrap-servers}")
private String bootstrapServers;
@Bean
public Map<String, Object> producerConfigs() {
Map<String, Object> props = new HashMap<>();
// list of host:port pairs used for establishing the initial connections to the Kakfa cluster
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,
bootstrapServers);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
JsonSerializer.class);
return props;
}
@Bean
public ProducerFactory<String, Object> producerFactory() {
return new DefaultKafkaProducerFactory<>(producerConfigs());
}
@Bean
public KafkaTemplate<String, Object> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试发送类的对象 com.example.project.web.routes.dto.RouteDto
在 …
spring apache-kafka json-deserialization spring-boot spring-kafka
我是 kafka 的新手,我浏览了文档,但我什么也不懂。有人可以解释一下什么时候使用这个ConcurrentKafkaListenerContainerFactory类吗?我已经使用过这个Kafkaconsumer类,但我看到ConcurrentKafkaListenerContainerFactory在我当前的项目中使用了它。请解释它的用途。
我在 Rest 控制器中使用可选参数,在我的例子中是为了区分要调用的方法:
@GetMapping("/cars")
@ResponseBody
public List<CarsDTO> getAllCarsByCat(@RequestParam Optional<Integer> cat1,
@RequestParam Optional<Integer> cat2) {
if (cat1.isPresent() && cat2.isPresent())
return carsService.getAllCarsByCat1AndCat2(cat1.get(), cat2.get());
else if (cat1.isPresent())
return carsService.getAllCarsByCat1(cat1.get());
else if (cat2.isPresent())
return carsService.getAllCarsByCat2(cat2.get());
else
return carsService.getAllCars();
}
Run Code Online (Sandbox Code Playgroud)
为什么下面线程的最高投票响应提出“使用可选参数在方法内部导致条件逻辑实际上是适得其反的。”?
我正是这样做的,并将其视为最具可读性和直接的解决方案。这种方法有什么不好?
我使用的龙目岛在我的项目和产生Setters和Getters使用@Setters,并@Getters注解POJO类的顶部。我正在尝试重写属性的setter方法,但它不起作用
我想检查JSON属性是否为空或空,我想在Setter方法中设置默认值
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ToString
public class DefaultModel {
private String name;
@Setter(AccessLevel.NONE)private String age;
public void setAge(String age) {
if(age==null||age.trim().isEmpty()||age.equals("null")) {
this.age="10";
}else {
this.age=age;
}
}
}
Run Code Online (Sandbox Code Playgroud)
工作方案:
{
"name":"some",
"age":null
}
{
"name":"some",
"age":"null"
}
{
"name":"some",
"age":" "
}
Run Code Online (Sandbox Code Playgroud)
失败的情况:
{
"name":"some"
}
Run Code Online (Sandbox Code Playgroud)
输出:
DefaultModel(name=some, age=null)
Run Code Online (Sandbox Code Playgroud)
我也在这里作为参考,但到目前为止还没有运气
目前我有这样的代码:
KafkaTemplate<String, String> kafkaTemplate;
List<Pet> myData;
for(Pet p: myData) {
String json = objectWriter.writeValueAsString(p)
kafkaTemplate.send(topic, json)
}
Run Code Online (Sandbox Code Playgroud)
所以每个列表项都是一一发送的。如何一次发送整个列表?
我是Kafka的新手,我正在尝试使用AdminClientAPI来管理在本地计算机上运行的Kafka服务器.我的设置与Kafka文档的快速入门部分完全相同.唯一的区别是我没有创建任何主题.
我在此设置上运行任何shell脚本都没有问题,但是当我尝试运行以下java代码时:
public class ProducerMain{
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,
"localhost:9092");
try(final AdminClient adminClient =
KafkaAdminClient.create(props)){
try {
final NewTopic newTopic = new NewTopic("test", 1,
(short)1);
final CreateTopicsResult createTopicsResult =
adminClient.createTopics(
Collections.singleton(newTopic));
createTopicsResult.all().get();
}catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误: TimeoutException: Timed out waiting for a node assignment
Exception in thread "main" java.lang.RuntimeException: org.apache.kafka.common.errors.TimeoutException: Timed out waiting for a node assignment.
at ProducerMain.main(ProducerMain.java:41)
<br>Caused …Run Code Online (Sandbox Code Playgroud) jlink compress选项有什么作用?oracle文档对此并不十分详细:
Enable compression of resources:
0: No compression
1: Constant string sharing
2: ZIP
Run Code Online (Sandbox Code Playgroud)
压缩哪些资源?有什么缺点--compress=2吗?
java ×8
apache-kafka ×4
spring ×3
spring-boot ×3
spring-kafka ×3
java-8 ×2
java-9 ×1
java-stream ×1
jlink ×1
lombok ×1
option-type ×1