我在我的项目中使用 Spring boot Jackson 依赖项和 lombok,作为响应,由于下划线,我得到了重复的字段
这是我的模型类:
@Getter
@Setter
@Accessors(chain = true)
@NoArgsConstructor
@ToString
public class TcinDpciMapDTO {
@JsonProperty(value = "tcin")
private String tcin;
@JsonProperty(value = "dpci")
private String dpci;
@JsonProperty(value = "is_primary_tcin_in_dpci_relation")
private boolean is_primaryTcin = true;
}
Run Code Online (Sandbox Code Playgroud)
如果我在is_primaryTcin字段中使用下划线,我会收到以下带有重复字段的响应
{
"_primaryTcin": true,
"tcin": "12345",
"dpci": "12345",
"is_primary_tcin_in_dpci_relation": true
}
Run Code Online (Sandbox Code Playgroud)
如果我从字段中删除下划线,isprimaryTcin那么我会得到正确的响应
{
"tcin": "12345",
"dpci": "12345",
"is_primary_tcin_in_dpci_relation": true
}
Run Code Online (Sandbox Code Playgroud)
这是因为下划线吗?但下划线更喜欢在变量名中使用,对吧?
有一个耗时的操作(大约10分钟),但卡夫卡在5分钟后重新平衡,甚至我暂停了消费者.消费者方法:
@KafkaListener(topics = {TopicAppoint.EXECUTE_SCHOOL_DATA_STATICS_TASK})
public void receiveMessage(@Payload String payload, Consumer<String, String> consumer) {
Set<TopicPartition> assignment = consumer.assignment();
consumer.pause(assignment);
if (StringUtils.isNotEmpty(payload)) {
SchoolStatisticsTaskDTO staticsTaskDTO = JSONObject.parseObject(payload, SchoolStatisticsTaskDTO.class);
Optional<SchoolStatisticsTaskDO> taskOptional = schoolStatisticsTaskRepository.findById(staticsTaskDTO.getTrackId());
taskOptional.ifPresent(schoolStaticsTaskDO -> {
// handler
});
}
consumer.resume(assignment);
}
Run Code Online (Sandbox Code Playgroud)
这是我的配置:
kafka:
bootstrap-servers: 192.168.0.230:9092
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
retries: 3
properties:
max.request.size: 12582912
consumer:
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
group-id: dc-fitness-data-consumer-group
properties:
max.partition.fetch.bytes: 12582912
#enable-auto-commit: false
listener:
ack-mode: record
concurrency: 6
Run Code Online (Sandbox Code Playgroud)
日志
13:09:20.219 [org.springframework.kafka.KafkaListenerEndpointContainer#2-0-C-1] INFO o.a.k.c.c.i.AbstractCoordinator - [Consumer clientId=consumer-25, groupId=dc-fitness-data-consumer-group] Attempt …Run Code Online (Sandbox Code Playgroud) 我有一个具有 Id 属性的实体“任务”,但我不需要在 JSON 文件中返回该字段。
@Entity
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonIgnore
private Integer Id;
@JsonProperty("task")
private String taskName;
private String status;
//getter and setter
}
Run Code Online (Sandbox Code Playgroud)
但是,当我发出 get 请求时,注释 @JsonIgnore 不会过滤该字段,如下所示:
{
"status": "started",
"timestamps": {
"submitted": "2018-12-31T00:34:20.718+0000",
"started": "2018-12-31T00:34:20.718+0000",
"completed": "2018-12-31T00:34:20.718+0000"
},
"id": 40001,
"task": "q094hiu3o"
}
Run Code Online (Sandbox Code Playgroud)
防止显示“Id”的正确方法是什么?
我有这种格式的日期yyyy-MM-dd'T'HH:mm:ss'Z',但是当我使用OffsetDateTime.parse(date);它解析它时,它通过消除返回字符串seconds
逻辑:从日期获取日期,如果是Saturday或Sunday将日期更改为星期一并返回日期字符串
String date = "2018-12-30T06:00:00Z";
System.out.println(date);
try {
OffsetDateTime dateTime = OffsetDateTime.parse(date);
System.out.println(dateTime); //2018-12-30T06:00Z
DayOfWeek day = dateTime.getDayOfWeek();
// check if price change date is Sunday or Saturday and change it to Monday
if (day.equals(DayOfWeek.SATURDAY) || day.equals(DayOfWeek.SUNDAY)) {
String finalDateTime = dateTime.with(TemporalAdjusters.next(DayOfWeek.MONDAY)).toString();
System.out.println(finalDateTime); //2018-12-31T06:00Z
}else {
System.out.println(date);
}
}catch(Exception ex) {
System.out.println(ex);
System.out.println(date);
}
Run Code Online (Sandbox Code Playgroud)
我需要以相同的输入格式返回字符串 yyyy-MM-dd'T'HH:mm:ss'Z'
我想app.kubernetes.io/part-of: myapp在我的 Spring Boot 管理应用程序中配置 kubernetes 发现的服务标签。
app.kubernetes.io/part-of是一个推荐的标签,所以我认为应该有一种方法来配置它以进行服务发现。
spring:
cloud:
kubernetes:
discovery:
namespace: myspace
all-namespaces: true
service-labels:
'app.kubernetes.io/part-of': myapp
Run Code Online (Sandbox Code Playgroud)
不幸的是,我在执行器中注意到,yaml 键中的所有特殊字符都被删除了configprops。
我的问题:
提示:
我使用 Spring boot 设置了项目,在启动时加载各个组件。每个单独的包都包含自己的数据源、进程等。我可以简单地使用它,它工作得很好
@SpringBootApplication(scanBasePackages = {
"com.package1",
"com.package2",
"com.package3"
})
public class Application extends SpringBootServletInitializer{
public static void main(String[] args){
SpringApplication.run(Application.class,args)
}
}
Run Code Online (Sandbox Code Playgroud)
但目前,单个项目的数量正在变得越来越大。是否可以将要扫描的组件/包列表放入外部属性文件或 spring Vault 中?我不确定如何检索它,是否可以在启动前检索属性?
编辑:
目前我尝试过这个:
@Import(AppConfig.class)
public class Application extends SpringBootServletInitializer{
public static void main(String[] args){
SpringApplication.run(Application.class,args)
}
}
@Configuration
@ComponentScan(basePackages = {$app.packages})
@EnableAutoConfiguration
public class AppConfig {
}
//in my properties file
app.packages = ["com.package1","com.package2","com.package3"]
Run Code Online (Sandbox Code Playgroud)
但它不起作用
我有一个非常特殊的问题,我想在哪里解析"2019-12-25T17:00:00-05:00"
,以便它可以给我结果DEC 12 | Thursday | 5:00pm
我通过使用DateTimeFormatter和尝试了以下代码LocalDate
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz", Locale.US);
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("MM d | E | hh:mm a", Locale.US);
LocalDate date = LocalDate.parse("2019-12-25T17:00:00-05:00", inputFormatter);
String formattedDate = outputFormatter.format(date);
contentTextView.setText(formattedDate);
Run Code Online (Sandbox Code Playgroud)
但是它崩溃了 DateTimeParseException: Text '2019-12-25T17:00:00-05:00' could not be parsed at index 19
知道为什么会崩溃以及我的输出是否可以提供预期的结果吗?谢谢!
一些列表在这里
List<Book> list = new ArrayList<>();
{
list.add(new Book("Core Java", 200));
list.add(new Book("Core Java", 500));
list.add(new Book("Core Java", 800));
list.add(new Book("Learning Freemarker", 150));
list.add(new Book("Learning Freemarker", 1350));
list.add(new Book("Learning Freemarker", 1250));
list.add(new Book("Spring MVC", 300));
list.add(new Book("Spring MVC", 600));
list.add(new Book("Spring MVC", 1600));
}
Run Code Online (Sandbox Code Playgroud)
我想显示这样的书单
Core Java", 800
Learning Freemarker", 1350
Spring MVC", 1600
Run Code Online (Sandbox Code Playgroud)
每1个元素
list .stream().distinct()
.sorted(Comparator.comparing(Book::bookname)
.thenComparing(Book::getPrice)).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
此代码仅排序。
我是单元测试的新手。参考谷歌后,我创建了一个测试类来测试我的控制器,如下所示:
@RunWith(SpringRunner.class)
@WebMvcTest(PromoController.class)
public class PromoApplicationTests {
@Autowired
protected MockMvc mvc;
@MockBean PromoService promoService;
protected String mapToJson(Object obj) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(obj);
}
protected <T> T mapFromJson(String json, Class<T> clazz)
throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(json, clazz);
}
@Test
public void applyPromotionTest_1() throws Exception {
String uri = "/classPath/methodPath";
List<Cart> cartLs = new ArrayList<Cart>();
// added few objects to the list
String inputJson = mapToJson(cartLs);
MvcResult mvcResult = …Run Code Online (Sandbox Code Playgroud) 我有一个ProducerRecord对象。
ProducerRecord<String, byte[]> hdr = addHeader.addMDGHeader(record);
Run Code Online (Sandbox Code Playgroud)
我正在尝试编写一个测试来检查特定标头键是否存在。
如果我打印hdr.headers().toString()我会得到以下内容RecordHeaders(headers = [RecordHeader(key = mdpHeader, value = [123, 34, 83, 101, 113, 117, 101, 110, 99, 101, 78, 111, 34, 58, 48, 44, 34, 84, 101, 109, 112, 108, 97, 116, 101, 115, 34, 58, 91, 93, 125])], isReadOnly = false)。
我该如何拔出mdpHeader?
java ×9
spring-boot ×5
apache-kafka ×2
jackson ×2
java-8 ×2
android ×1
date ×1
foreach ×1
hibernate ×1
java-date ×1
java-stream ×1
java-time ×1
json ×1
lombok ×1
package ×1
spring-kafka ×1
spring-vault ×1