Spring - 问题汇总
JSON parse error: Cannot deserialize value of type `java.util.Date` from String
@DateTimeFormat(pattern = "yyyy-MM-dd") @JsonFormat(pattern = "yyyy-MM-dd") private Date date;
Spring项目启动报WARNING:Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [xxxx] milliseconds
原因
SHA1PRNG算法是基于SHA-1算法实现且保密性较强的伪随机数生成器,在SHA1PRNG中,有一个种子产生器,它根据配置执行各种操作。
- 如果Java.security.egd属性或securerandom.source属性指定的是”file:/dev/random”或”file:/dev/random”,那么JVM会使用本地种子产生器NativeSeedGenerator,它会调用super()方法,即调用SeedGenerator.URLSeedGenerator(/dev/random)方法进行初始化。
- 如果java.security.egd属性或securerandom.source属性指定的是其它已存在的URL,那么会调用SeedGenerator.URLSeedGenerator(url)方法进行初始化。
这就是为什么我们设置值为”file:/dev/random”或者值为”file:/dev/./random”都会起作用的原因。
在这个实现中,产生器会评估熵池(entropy pool)中的噪声数量。随机数是从熵池中进行创建的。当读操作时,/dev/random设备会只返回熵池中噪声的随机字节。/dev/random非常适合那些需要非常高质量随机性的场景,比如一次性的支付或生成密钥的场景。
当熵池为空时,来自/dev/random的读操作将被阻塞,直到熵池收集到足够的环境噪声数据。这么做的目的是成为一个密码安全的伪随机数发生器,熵池要有尽可能大的输出。对于生成高质量的加密密钥或者是需要长期保护的场景,一定要这么做。
那么什么是环境噪声?
随机数产生器会手机来自设备驱动器和其它源的环境噪声数据,并放入熵池中。产生器会评估熵池中的噪声数据的数量。当熵池为空时,这个噪声数据的收集是比较花时间的。这就意味着,Tomcat在生产环境中使用熵池时,会被阻塞较长的时间。
解决
1)在Tomcat环境中解决
可以通过配置JRE使用非阻塞的Entropy Source。在catalina.sh中加入
-Djava.security.egd=file:/dev/./urandom
加入后再启动Tomcat,整个启动耗时下降到Server startup in 2912 ms。
2)在JVM环境中解决
打开$JAVA_PATH/jre/lib/security/java.security这个文件,找到下面的内容:
securerandom.source=file:/dev/random
替换成
securerandom.source=file:/dev/./urandom
Springboot解决The valid characters are defined in RFC 7230 and RFC 3986
增加配置类如下:
@Configuration public class ServerConfig { @Bean public ConfigurableServletWebServerFactory webServerFactory() { TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); factory.addConnectorCustomizers(new TomcatConnectorCustomizer() { @Override public void customize(Connector connector) { connector.setProperty("relaxedQueryChars", "|{}[]");//允许的特殊字符 } }); return factory; } }
java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result异常的解决方法
JAVA中用BigDecimal做除法的时候一定要在divide方法中传递第二个参数,定义精确到小数点后几位。否则在不整除的情况下,结果是无限循环小数时,就会抛出以上异常。
解决方法:
bigDecimalObj.divide(bar, 2, BigDecimal.ROUND_HALF_UP);
divide方法有两个重载的方法,一个是传两个参数的,一个是传三个参数的:
二参数的方法:
- @param divisor value by which this {@code BigDecimal} is to be divided. 传入除数
- @param roundingMode rounding mode to apply. 传入round的模式
三参数的方法:
- @param divisor value by which this {@code BigDecimal} is to be divided. 传入除数
- @param scale scale of the {@code BigDecimal} quotient to be returned. 传入精度
- @param roundingMode rounding mode to apply. 传入round的模式
The field file exceeds its maximum permitted size of 1048576 bytes
SpringBoot内嵌的 tomcat 默认允许上传的文件大小为1M,超出这个就会报错。
在配置文件中增加如下内容,并重启服务
spring: servlet: multipart: max-file-size: 20MB #单个文件大小 max-request-size: 100MB #总数据大小
Springboot Gateway服务启动报错:com.google.common.collect.Sets$SetView.iterator()
完整报错为:
java.lang.NoSuchMethodError: com.google.common.collect.Sets$SetView.iterator()Lcom/google/common/collect/UnmodifiableIterator;
这个报错原因是没有引入 com.google.guava 包,造成报错。在pom文件中添加依赖即可:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1-jre</version>
</dependency>
Springboot 服务启动报错:org.springframework.web.context.support.ServletContextAwareProcessor: method <init>()V not found
原因:spring-1.2.6.jar 和 spring-web-4.3.6-RELEASE.jar 均含有ServletContextAwareProcessor类。通过排除依赖的方式让项目中只保留一个ServletContextAwareProcessor。
<exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions>