Java Problems Summary
Maven Related Problems
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
MyBatis Related Problems
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
MybatisPlus Invalid bound statement (not found) exception
IDEA Usage Related Problems
idea terminal Chinese garbled characters
# 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
- 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);
}
}
- If cannot extend, consider skipping
Beanserialization step, such as using method to convert Bean to Map - If really no way, use
FastjsonorGjsoninstead
Second field uppercase problem
- 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."
- Use
@JsonAutoDetect
@JsonAutoDetect(
fieldVisibility = JsonAutoDetect.Visibility.ANY,
getterVisibility = JsonAutoDetect.Visibility.NONE,
setterVisibility = JsonAutoDetect.Visibility.NONE)
Members used in serialization and deserialization
| Usage Location | Influence Direction | Common Usage |
|---|---|---|
| Field | Serialization + Deserialization | Force include private fields |
| getter | Serialization | Change output field name |
| setter | Deserialization | Change input field name |
| Constructor Parameter | Deserialization | Map JSON parameter name to constructor parameter |
| record Parameter | Serialization + Deserialization | Control 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
LocalDateTimesupport varies across versions. In production, prefer converting toDateorStringat the API boundary.
2. Spring Boot global config
application.yml (Recommended)
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
| Type | Timezone | Use case |
|---|---|---|
Date | Yes (UTC timestamp) | Cross-timezone transfer, JSON serialization |
Instant | Yes (UTC) | Service-internal time calculation, logging |
ZonedDateTime | Yes | Business logic that needs timezone |
OffsetDateTime | Yes | API responses, cross-system transfer |
LocalDateTime | No | Display 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
- 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.
- 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.