Lombok @Builder JSON Conflict, Dynamic Log Levels, JWT Overview, and Java 8 parallelStream Tips
The brief explains how Lombok’s @Builder can clash with JSON libraries and how adding no‑args and all‑args constructors resolves it, shows how to adjust log levels at runtime via Arthas or Spring’s LoggingSystem, outlines JWT structure and security cautions, and warns of common Java 8 parallelStream performance and correctness pitfalls.
This issue brief covers four practical Java backend topics: the Lombok @Builder annotation conflict with JSON deserialization, dynamic adjustment of log output levels, the fundamentals and pitfalls of JWT, and common traps when using Java 8 parallelStream.
Lombok @Builder and JSON deserialization
Lombok’s @Builder simplifies object creation, but when combined with fastjson the generated builder class only provides a package‑private all‑args constructor. fastjson prefers a public no‑arg constructor, causing a deserialization failure.
@Builder
@Data
public class ItemData {
/** 商品id */
public Long itemId;
/** 商品标题 */
public String itemTitle;
} ItemData itemData = ItemData.builder()
.itemId(123456L)
.itemTitle("闲小鱼")
.build();
String itemString = JSONObject.toJSONString(itemData);
ItemData deserializeItem = JSON.parseObject(itemString, ItemData.class);Because the generated constructor is not public, fastjson cannot instantiate the class. The recommended fix is to add @NoArgsConstructor and @AllArgsConstructor alongside @Builder, which supplies both a public no‑arg and an all‑args constructor, making the class compatible with fastjson, Jackson, Gson, etc.
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ItemData {
public Long itemId;
public String itemTitle;
}Dynamic Log Level Adjustment
Log levels are defined by the underlying logging framework (Log4j, Logback). For a single machine you can use Alibaba’s Arthas tool:
logger --name root --level debug -c 2dde1bffFor a cluster, integrate a configuration center with Spring Boot’s LoggingSystem to change levels at runtime. Example service:
@Service
public class LoggerLevelAdjustService {
@Autowired
private LoggingSystem loggingSystem;
public void setRootLoggerLevel(String newLevel) {
setLoggerLevel("ROOT", newLevel);
}
public void setLoggerLevel(String loggerName, String newLevel) {
if (StringUtils.isEmpty(newLevel)) return;
for (LogLevel level : LogLevel.values()) {
if (level.name().equals(newLevel)) {
loggingSystem.setLogLevel(loggerName, level);
}
}
}
}Invoke this service when the configuration center pushes a new log‑level value.
JWT Generation Principles and Common Misconceptions
JWT (JSON Web Token) consists of Header, Payload, and Signature, concatenated with dots. The Header declares the signing algorithm (e.g., HS256). The Payload carries claims (e.g., userId) and is only Base64Url‑encoded, not encrypted, so it must not contain sensitive data such as passwords. The Signature is computed as:
Signature = HS256(base64UrlEncode(header) + "." + base64UrlEncode(payload), secret)If the Payload is tampered with, the signature verification will fail, ensuring integrity.
Java 8 parallelStream Pitfalls
parallelStream runs tasks in a ForkJoinPool (commonPool) whose size defaults to CPU cores - 1 . This can lead to unexpected results and performance issues:
Reduction with an identity value may produce different results between sequential and parallel streams because the identity is applied per sub‑task.
Parallelism benefits only stateless operations (map, filter). State‑ful or ordering operations (sorted, distinct, limit) may degrade performance.
Long‑running tasks can block the commonPool, affecting other parallel streams. Use a dedicated ForkJoinPool for such workloads.
ForkJoinPool businessPool = new ForkJoinPool(4);
int sum = businessPool.submit(() ->
IntStream.of(1, 2, 3).parallel().reduce(0, Integer::sum)
).get();
businessPool.shutdown();Xianyu Technology
Official account of the Xianyu technology team
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.