跳到主要内容

Java问题汇总

Maven相关问题

处理Maven远程仓库下载不到jar包问题

先去Maven Repository找到对应的依赖,并下载。

去下载目录下执行如下命令,注意替换掉-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相关问题

SQL查询时 null值封装回int类型报 UncategorizedSQLException

将BO属性从int改为Integer类型

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

Mysql tinyint有毒

mysql 不要使用字段类型tinyint 可能你更新的1会变成49哦

参见博客

MybatisPlus 出现 Invalid bound statement (not found) 异常

参见文章


IDEA使用相关问题

idea terminal 中文乱码问题

参考文章

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

其他问题

动态数据源问题

如果所用的操作有事务,则需要在Dao层之前,对数据源进行设置,在Dao或者Mybatis-plus中的带dao的service都会切换数据源失效。

jackson序列化问题

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"])

先排查是哪里报的

  1. 如果是可扩展Spring Boot的:直接扩展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. 如果无法扩展,考虑跳过Bean序列化步骤,比如用其实转化Bean为Map的方法
  2. 实在没有办法,改用Fastjson或者Gjson

第二个字段大写问题

  1. 使用 @JsonProperty

@JsonProperty 用来明确告诉 Jackson:

“这个字段(或方法、构造参数)要作为 JSON 属性使用,并且属性名是 XXX。”

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

序列化和反序列化使用到的成员

用法位置影响方向常见用途
字段序列化 + 反序列化强制包含私有字段
getter序列化改输出字段名
setter反序列化改输入字段名
构造函数参数反序列化映射 JSON 参数名到构造参数
record 参数序列化 + 反序列化控制 JSON 属性名

Fastjson Date 和 LocalDateTime 的时区问题

Date 默认使用 JVM 时区序列化;LocalDateTime 不带时区信息,行为取决于 Fastjson 版本和 JVM 配置。跨环境部署时时区不一致是主要风险。

1. 字段级别指定

DTO/VO 字段上通过 @JSONField 声明格式和时区,接口层统一约定:

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

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

Fastjson 对 LocalDateTime 的原生支持在不同版本间有差异,生产环境建议统一转 DateString 再输出。

2. Spring Boot 全局配置

application.yml(推荐)
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: Asia/Shanghai
Fastjson 全局配置
// 方式一:全局默认值
JSON.defaultTimeZone = TimeZone.getTimeZone("Asia/Shanghai");
JSON.defaultDateFormat = "yyyy-MM-dd HH:mm:ss";

// 方式二: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. 不指定时区的表现

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

LocalDateTime 没有时区信息,序列化后不带 +08:00Z

LocalDateTime ldt = LocalDateTime.of(2024, 1, 1, 12, 0, 0);
JSON.toJSONString(ldt); // "2024-01-01 12:00:00"
// 后续转 Instant 必须显式指定 ZoneId
Instant instant = ldt.atZone(ZoneId.of("Asia/Shanghai")).toInstant();

4. 类型选择建议

类型时区用途
Date有(UTC 时间戳)跨时区传输、JSON 序列化
Instant有(UTC)服务内部时间计算、日志
ZonedDateTime需要保留时区的业务逻辑
OffsetDateTimeAPI 响应、跨系统传输
LocalDateTime仅展示层,不用于传输或计算

服务内部优先使用 Instant / ZonedDateTime / OffsetDateTime,避免裸 DateLocalDateTime 直接做 JSON 传输。

数据库连接池配置问题

使用Druid 连接池时, 配置参数 validationInterval必须小于 timeBetweenEvictionRunMillis, 保证数据库连接一直是可用的。

API接口规范

  • 查询数据量、
  • 入参参数检查、
  • 响应报文中大整数,浮点数字段使用字符串类型
  • 入参类型不为基本类型
协议
本作品代码部分采用Apache 2.0协议 进行许可。遵循许可的前提下,你可以自由地对代码进行修改,再发布,可以将代码用作商业用途。但要求你:
  • 署名:在原有代码和衍生代码中,保留原作者署名及代码来源信息。
  • 保留许可证:在原有代码和衍生代码中,保留Apache 2.0协议文件。
本作品文档部分采用知识共享署名 4.0 国际许可协议 进行许可。遵循许可的前提下,你可以自由地共享,包括在任何媒介上以任何形式复制、发行本作品,亦可以自由地演绎、修改、转换或以本作品为基础进行二次创作。但要求你:
  • 署名:应在使用本文档的全部或部分内容时候,注明原作者及来源信息。
  • 非商业性使用:不得用于商业出版或其他任何带有商业性质的行为。如需商业使用,请联系作者。
  • 相同方式共享的条件:在本文档基础上演绎、修改的作品,应当继续以知识共享署名 4.0国际许可协议进行许可。