1、 applicationContext.xml路径问题
默认在web-inf下,开发中配置在src下,需要在web.xml中配置路径
注意,是classpath而不是classPath
2、 Failed to read candidate component class:
Spring 版本过低,不支持当前JDK版本
3、jar部署方式中文件读写
可以采用相对路径的方式实现文件存储:
public static String getExportFileName(String prefixFileName, FileType fileType) {
String fileName = prefixFileName + "-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
return "./test" + "/" + fileName + "." + fileType.getExtension();
}
public static String saveImportFile(MultipartFile file, Long statementBatchId) {
String saveFileName = getExportFileName("批次" + statementBatchId + "导入", FileType.XLS);
File saveFile = new File(saveFileName);
try {
FileUtils.writeByteArrayToFile(saveFile, file.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
return saveFileName;
}
4、多线程,或者事务过大,可以采用手动事务
@Autowired
private PlatformTransactionManager platformTransactionManager;
@Autowired
private TransactionDefinition transactionDefinition;
TransactionStatus transactionStatus = platformTransactionManager.getTransaction(transactionDefinition);
try {
save = packageStatementItemService.save(itemEntity);
StopWatch watch = new StopWatch();
watch.start();
statementCheckingService.checkingPackageAfterImport(itemEntity, userId, userName);
watch.stop();
platformTransactionManager.commit(transactionStatus);
log.info(LogTempalte.TIME_LOG_DEBUG,"包裹对账,单条重新匹配用时",watch.getTotalTimeSeconds());
} catch (Exception e) {
log.error(e.getMessage());
error.add(String.format("系统异常【%s】", e.getMessage()));
save = false;
platformTransactionManager.rollback(transactionStatus);
}
5、PKIX path building failed(SSL证书未被信任)
解决方案:1、信任证书
keytool -importcert -alias "passport" -file /xx.crt -keystore /srv/java/jdk1.8.0_171/jre/lib/security/cacerts -storepass changeit
2、忽略证书
public void afterPropertiesSet() throws Exception {
requestFactory = new HttpComponentsClientHttpRequestFactory(){
private HttpClientContext context;
{
log.info("passport账号为:" + passportConfig.getApplicationAccessKey() + " 密码为:" + passportConfig.getApplicationAccessPassword());
context = HttpClientContext.create();
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(org.apache.http.auth.AuthScope.ANY,
new org.apache.http.auth.UsernamePasswordCredentials(passportConfig.getApplicationAccessKey(), passportConfig.getApplicationAccessPassword()));
context.setCredentialsProvider(credsProvider);
}
@Override
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
return context;
}
};
requestFactory.setHttpClient(getHttpsClient());
restTemplate = new RestTemplate(requestFactory);
}
public static CloseableHttpClient getHttpsClient() {
CloseableHttpClient httpClient;
SSLContext sslContext = null;
try {
sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
}).build();
} catch (NoSuchAlgorithmException e) {
e.getStackTrace();
} catch (KeyManagementException e) {
e.getStackTrace();
} catch (KeyStoreException e) {
e.getStackTrace();
}
httpClient = HttpClients.custom().setSSLContext(sslContext).
setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
return httpClient;
}
6、com java.lang.NoSuchMethodError:XXX
通常是由于包冲突导致
解决方案有两个:a:去掉多余的版本 b:利用dependencyManager控制版本