Java相关问题处理
SpringBoot相关问题
Springboot启动时报错 org.springframework.boot不存在
建议采用方法二在终端中输入mvn idea:idea
,方法一在Spring Boot 项目中,运行其他Main方法测试类的时候会重新启动SpringBoot 工程,如果你还从ApplicationContext中获取,那么将会收到报错容器已经关闭。
参见博客
spring boot源码运行SpringApplication时报错
Could not find artifact lifecycle-mapping:lifecycle-mapping:jar:sources:1.0.0 参见博客 已经要付费看了!无语
gradle spring boot 3.0 导入报错
确保gradle的版本JDK 17 和 JDK 的版本正确。
logback相关
- 启动控制台日志没有颜色
在spring-logback.xml中引入
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework.web" level="INFO"/>
<logger name="org.springboot.sample" level="TRACE"/>
- logback 修改日志打印内容
代码: extends ThrowableHandlingConverter impl convert 方法
logback配置
<conversionRule convertionWord=“someKeyMsg” convertClass=“com.xxx.xxx.Xclass”/>
mapstruct相关
一直使用unmappedTargetPolicy Error, 可以避免很多问题。
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
使用Tomcat启动服务出现依赖冲突问题
- 解决方案 查看报错信息 根据提示找到冲突的类,定位到对应的jar中(直接搜索类名就行了 including no-project items)
- 去tomcat lib目下查看相关jar;去项目导入的依赖中找到jar。
- 首先要确定 jar的版本 与当前工程相关的东西是否匹配,如果匹配,则可以进行去除一边的jar包进行测验。一般都能解决问题
after run the packaged jar occurs this unable to find main class
方法1. 替换插件
<!--<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>none</mainClass> <!– 取消查找本项目下的Main方法:为了解决Unable to find main class的问题 –>
<classifier>execute</classifier> <!– 为了解决依赖模块找不到此模块中的类或属性 –>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
方法2: 去掉spring-boot parent,再使用spring boot plugin 打包
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类型
rg.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"
mojibake chinese characters in Resttemplate
When you use Spring Boot mvc, provide a api endpoint,
maybe confront mojibake chinese characters.
It's not the problem of Spring Boot, but what the client you use.
result form is below.
server receive content-type | client Content-Type | client Accept-Charset | lib use | worked? |
---|---|---|---|---|
application/json; charset=UTF-8 | application/json | charset=UTF-8 | RestTemplate | Yes |
application/json; charset=UTF-8 | application/json; charset=UTF-8 | RestTemplate | Yes | |
application/json; charset=UTF-8 | application/json | charset=UTF-8 | HttpClient | Yes |
application/json; charset=UTF-8 | application/json; charset=UTF-8 | HttpClient | Yes | |
text/plain; charset=UTF-8 | application/json | charset=UTF-8 | RestTemplate | No |
text/plain; charset=UTF-8 | application/json; charset=UTF-8 | RestTemplate | Yes | |
text/plain; charset=UTF-8 | application/json | charset=UTF-8 | HttpClient | Yes |
text/plain; charset=UTF-8 | application/json; charset=UTF-8 | HttpClient | Yes |
finally conclusion about the form , only when you use RestTemplate, and separately set the Content-Type
and Accept-Charset
, the problem will occur.
thanks for your reading.
If you have any problem, feel free to contact with me.
其他(不便归类)
jasper后台,如果打印的模板需要动态区太多,以前封装的框架不够用,怎么办?
这个时候重写框架 ,可能太令人f**k了。当然你也可以找新的版本的POI框架 看看有没有相应的扩展点。
此处不讨论新POI框架的内容;采取只修改jasper文件的方案:
- 选择文本框一定要是
text field
不能是static text
- 在框内写上三元表达式,然后勾选上
remove line when blank
andblank when null
- 预览效果 此处是表达式为
1==2?"a":null
的效果图
如何配置生成日志文件
<!--日志文件主目录:这里${user.home}为当前服务器用户主目录-->
<property name="LOG_HOME" value="log"/>
<!--配置日志文件(File)-->
<appender name="rollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--设置策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!--日志文件路径:这里%d{yyyyMMdd}表示按天分类日志-->
<FileNamePattern>${LOG_HOME}/%d{yyyyMMdd}.%i.log</FileNamePattern>
<maxFileSize>30MB</maxFileSize>
<!--<maxHistory>100</maxHistory>-->
<!--<totalSizeCap>5GB</totalSizeCap>-->
</rollingPolicy>
<!--设置格式-->
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<!-- 或者使用默认配置 -->
<!--<pattern>${FILE_LOG_PATTERN}</pattern>-->
<charset>utf8</charset>
</encoder>
<!--日志文件最大的大小-->
<!--<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">-->
<!--<MaxFileSize>1KB</MaxFileSize>-->
<!--</triggeringPolicy>-->
</appender>
<root level="info">
<appender-ref ref="rollingFile" />
</root>
MockBean使用问题
MockBean使用时,String 入参,int返回值,传入参数为实际调用的方法时,thenReturn为期望值,否则当传入字符串时始终为0;
lombok使用的坑
@Data
注解生成的getters
, setters
和jackson-databind
中的不一致导致反序列化时,对象的属性没有值。
动态数据源问题
如果所用的操作有事务,则需要在Dao层之前,对数据源进行设置,在Dao或者Mybatis-plus中的带dao的service都会切换数据源失效。
override by no-compatible bean instance of type[com.sun.Proxy.$Proxy227
原因:JDK代理和CGlib代理混用的问题
解决:注入接口,而不是某个实现类
netty响应速度问题
在关闭资源时,group.shutdownGracefully().sync()
,去掉.sync()
调用就不会阻塞。