工作开发中遇到的Java有关问题和解决方案

重要提示:此文章将持续更新,建议收藏


1、java.lang.Exception: Connector attribute SSLCertificateFile must be defined when using SSL with APR

原因: 使用tomcat7调整协议从http到https后,启动tomcat时报的错,原本protocol写的是HTTP/1.1
解决: tomcat7默认使用的是APR协议,更改8443端口的protocol协议为org.apache.coyote.http11.Http11Protocol

修改conf/server.xml

    <Connector SSLEnabled="true" acceptCount="100" clientAuth="false"
    disableUploadTimeout="true" enableLookups="false" maxThreads="150"
    port="8443" keystoreFile="D:\java_code\tomcat\tomcat.keystore" keystorePass="1q2w3e4r"
    protocol="org.apache.coyote.http11.Http11Protocol" scheme="https"
    secure="true" sslProtocol="TLS" />

2、org.hibernate.InstantiationException: No default constructor for entity: : com.su.shoppingb.domain.Cart

解决: Cart实体类没有默认的构造方法,添加上即可


3、java.nio.charset.MalformedInputException: Input length = 1 或者java.nio.charset.MalformedInputException: Input length = 2

解决: application.yml中 编码格式问题,重设为GBK即可;整个项目是UTF-8

mybatis: mapper-locations: classpath:/mappers/*.xml 填写完整


4、在mapper.xml中,resultType指向实体类,select出的结果别名要和实体类字段一样


5、Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)

解决: 无关紧要不理会,或者在templates目录下添加文件即可解决


6、保存用户时,java.sql.SQLSyntaxErrorException: Table ‘wx_driving.hibernate_sequence’ doesn’t exist

解决: 在该实体类的主键上加上
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)


7、微信小程序前端获取手机号信息时,“getPhoneNumber:fail Error: 该 appid 没有权限”

解决: 小程序号需要企业注册小程序并认证


8、HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@7bd3fbc (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.

解决:
在application.xml中添加spring.datasource.hikari
hikari:
connection-timeout: 60000
validation-timeout: 3000
idle-timeout: 60000
login-timeout: 5
max-lifetime: 60000
maximum-pool-size: 10
minimum-idle: 10
read-only: false
添加连接生命周期


9、后端服务换了新的appid后,获取openid出现 {“errcode”:40029,“errmsg”:“invalid code, hints: [ req_id: 0Hga3aMre- ]”}

解决: 前端和后端统一使用新的appid和appsecret


11、获取手机号时,调接口:org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter ‘encryptedData’ is not present

解决: 前端请求加: header: {
“Content-Type”: “application/x-www-form-urlencoded”
},


12、java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/CBC/PKCS7Padding

解决: AES/CBC/PKCS7Padding改成AES/CBC/PKCS5Padding


13、启用https org.springframework.context.ApplicationContextException: Failed to start bean ‘webServerStartStop’; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat server

解决: springboot2.x没有EmbeddedServletContainerFactory,已替换为TomcatServletWebServerFactory 添加以下代码:

@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
    
    
    return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
    
    
        @Override
        public void customize(ConfigurableWebServerFactory factory) {
    
    
            factory.setPort(443);
        }
    };
}
/**
* @Description:  将http重定向至https
* @param: "[]"
* @Return: org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory
* @Author: supenghui
* @Date: 2021/1/18 12:46
*/
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory(){
    
    
    TomcatServletWebServerFactory tomcat =new TomcatServletWebServerFactory(){
    
    
        @Override
        protected void postProcessContext(Context context) {
    
    
            SecurityConstraint securityConstraint=new SecurityConstraint();
            securityConstraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection=new SecurityCollection();
            collection.addPattern("/");
            securityConstraint.addCollection(collection);
            context.addConstraint(securityConstraint);
        }
    };
    tomcat.addAdditionalTomcatConnectors(createHttpConnector());
    return tomcat;
}
private Connector createHttpConnector() {
    
    
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    connector.setScheme("http");
    connector.setSecure(false);
    connector.setPort(80);
    connector.setRedirectPort(443);
    return connector;
}

14、测试微信小程序使用高德地图出现 fail url not in domain list

解决: 使用高德地图服务,需要把高的域名加到request中;域名: https://restapi.amap.com


15、各种图案的样式:https://css-tricks.com/the-shapes-of-css/


16、org.springframework.dao.InvalidDataAccessApiUsageException: No EntityManager with actual transaction available for current thread - cannot reliably process ‘remove’ call; nested exception is javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process ‘remove’ call

解决: 由于在需要事务的方法上,没有开启事务,所在在方法上添加@Transactional


17、Error No.1130 Host ‘192.168.25.3’ is not allowed to connect to this MySQL server

解决:

  1. 改表法
    可能是你的帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑登入MySQL后,更改"MySQL"数据库"user"表里的"host"项,将"localhost"改成"%"
    mysql -u root -pyourpwd
    mysql>use mysql;
    mysql>update user set host = ‘%’ where user =‘root’;
    mysql>select host, user from user;

18、调用微信支付接口时,total_fee必须为Integer


author:su1573
鄙人记录生活点滴,学习并分享,请多指教!!!
如需交流,请联系 [email protected],鄙人看到会及时回复

猜你喜欢

转载自blog.csdn.net/su1573/article/details/115216005