Skip to main content

Java Problems Summary

Solving Maven Remote Repository Jar Download Failures​

Go to Maven Repository to find the corresponding dependency and download it.

Go to the download directory and execute the following command, replacing -DgroupId, -DartifactId, -Dversion, -Dfile

mvn install:install-file -DgroupId=io.confluent -DartifactId=kafka-schema-registry-client -Dversion=4.1.0 -Dpackaging=jar -Dfile=kafka-schema-registry-client-4.1.0.jar

mvnw clean occurs Some Enforcer rules have failed​

Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:1.4.1:enforce
(enforce-versions) on project SIMI: Some Enforcer rules have failed. Look above for
specific messages explaining why the rule failed. -> [Help 1]

not solved! stackoverflow website

To skip enforcer (not always working)

mvn clean install -Denforcer.skip=true

To continue the build if error

mvn clean install -Denforcer.fail=false
./mvnw clean install -DskipDistribution=true -Denforcer.skip=true

UncategorizedSQLException when SQL query null value is mapped to int type​

Change BO property from int to Integer type

org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation;
uncategorized SQLException for SQL []; SQL state [null]; error code [0];

Mysql tinyint is toxic​

mysql do not use field type tinyint, possibly your update of 1 will become 49

See blog

MybatisPlus Invalid bound statement (not found) exception​

See article


idea terminal Chinese garbled characters​

Reference article

# solve idea terminal Chinese garbled
export LANG="zh_CN.UTF-8"
export LC_ALL="zh_CN.UTF-8"

Other Problems​

Dynamic Data Source Problems​

If the operation used has a transaction, you need to set the data source before the Dao layer. In Dao or Mybatis-plus, service with dao will switch data source invalidly.

jackson serialization problems​

LocalDateTime not supported by default​

Java 8 date/time type java.time.LocalDateTime not supported by default:
add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable
handling (through reference chain: java.util.ArrayList[0]->xxx.xxx.xxx.xxx.XxxxPo["updatetime"])

First troubleshoot where it is reported

  1. If Spring Boot can be extended: directly extend ObjectMapper
@Configuration
public class JacksonConfig implements WebMvcConfigurer {
@Override
public void extendMessageConverters(final List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.registerModule(new JavaTimeModule());
messageConverter.setObjectMapper(objectMapper);
converters.add(0, messageConverter);
}
}
  1. If cannot extend, consider skipping Bean serialization step, such as using method to convert Bean to Map
  2. If really no way, use Fastjson or Gjson instead

Second field uppercase problem​

  1. Use @JsonProperty

@JsonProperty is used to explicitly tell Jackson:

"This field (or method, constructor parameter) is to be used as a JSON property, and the property name is XXX."

  1. Use @JsonAutoDetect
@JsonAutoDetect(
fieldVisibility = JsonAutoDetect.Visibility.ANY,
getterVisibility = JsonAutoDetect.Visibility.NONE,
setterVisibility = JsonAutoDetect.Visibility.NONE)

Members used in serialization and deserialization

Usage LocationInfluence DirectionCommon Usage
FieldSerialization + DeserializationForce include private fields
getterSerializationChange output field name
setterDeserializationChange input field name
Constructor ParameterDeserializationMap JSON parameter name to constructor parameter
record ParameterSerialization + DeserializationControl JSON property name

Fastjson Date and LocalDateTime timezone issues​

Date serializes using the JVM default timezone; LocalDateTime has no timezone info, so its behavior depends on the Fastjson version and JVM settings. Cross-environment timezone mismatch is the main risk.

1. Field-level annotation​

Declare format and timezone on DTO/VO fields via @JSONField:

public class UserDTO {
@JSONField(format = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private LocalDateTime createTime;

// Date works the same way
@JSONField(format = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime;
}

Fastjson's LocalDateTime support varies across versions. In production, prefer converting to Date or String at the API boundary.

2. Spring Boot global config​

spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: Asia/Shanghai
Fastjson global config​
// Option 1: global defaults
JSON.defaultTimeZone = TimeZone.getTimeZone("Asia/Shanghai");
JSON.defaultDateFormat = "yyyy-MM-dd HH:mm:ss";

// Option 2: FastJsonConfig Bean
@Bean
public FastJsonConfig fastJsonConfig() {
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
config.setCharset(StandardCharsets.UTF_8);
return config;
}

3. Behavior without explicit timezone​

Date d = new Date(0L);
JSON.toJSONString(d);
// UTC env → "1970-01-01 00:00:00"
// Asia/Shanghai env → "1970-01-01 08:00:00"

LocalDateTime has no timezone info, so serialization omits +08:00 or Z:

LocalDateTime ldt = LocalDateTime.of(2024, 1, 1, 12, 0, 0);
JSON.toJSONString(ldt); // "2024-01-01 12:00:00"
// Must specify ZoneId explicitly when converting to Instant
Instant instant = ldt.atZone(ZoneId.of("Asia/Shanghai")).toInstant();

4. Type selection guide​

TypeTimezoneUse case
DateYes (UTC timestamp)Cross-timezone transfer, JSON serialization
InstantYes (UTC)Service-internal time calculation, logging
ZonedDateTimeYesBusiness logic that needs timezone
OffsetDateTimeYesAPI responses, cross-system transfer
LocalDateTimeNoDisplay only, not for transfer or calculation

Prefer Instant / ZonedDateTime / OffsetDateTime inside services. Avoid passing raw Date and LocalDateTime directly as JSON.

Database Connection Pool Configuration Problems​

When using Druid connection pool, config parameter validationInterval must be less than timeBetweenEvictionRunMillis, ensuring database connection is always available.

API Interface Standards​

  • Query data volume
  • Input parameter check
  • Response message large integer, floating point fields use string type
  • Input parameter type not primitive type
Agreement
The code part of this work is licensed under Apache License 2.0 . You may freely modify and redistribute the code, and use it for commercial purposes, provided that you comply with the license. However, you are required to:
  • Attribution: Retain the original author's signature and code source information in the original and derivative code.
  • Preserve License: Retain the Apache 2.0 license file in the original and derivative code.
The documentation part of this work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License . You may freely share, including copying and distributing this work in any medium or format, and freely adapt, remix, transform, and build upon the material. However, you are required to:
  • Attribution: Give appropriate credit, provide a link to the license, and indicate if changes were made.
  • NonCommercial: You may not use the material for commercial purposes. For commercial use, please contact the author.
  • ShareAlike: If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.