解決Cannot resolve com.alibaba.cloud:aliyun-oss-spring-boot-starter:unknown 檔案上傳報錯aliCloudEdasSdk解決
1.解決unknown:
<!--引入spring cloud alibaba-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--解決Cannot resolve com.alibaba.cloud:aliyun-oss-spring-boot-starter:unknown-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>aliyun-oss-spring-boot-starter</artifactId>
<version>1.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.執行上傳測試報錯
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘com.cevent.yameng.web.mall.product.YamengMallProductApplicationTests’: Unsatisfied dependency expressed through field ‘ossClient’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.aliyun.oss.OSSClient’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)
3.問題分析:阿里雲dependencies版本不相容
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'aliCloudEdasSdk' defined in class path resource [com/alibaba/cloud/spring/boot/context/autoconfigure/EdasContextAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.cloud.context.edas.AliCloudEdasSdk]: Factory method 'aliCloudEdasSdk' threw exception; nested exception is java.lang.NoSuchMethodError: com.aliyuncs.profile.DefaultProfile.getHttpClientConfig()Lcom/aliyuncs/http/HttpClientConfig;
4.修改common-pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>yameng-mall</artifactId>
<groupId>com.cevent.yameng.web.mall</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>yameng-mall-common</artifactId>
<description>各個微服務系統依賴的公共服務包,包括bean、utils等工具類</description>
<dependencies>
<!--引入mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<!--引入lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<!--引入httpStatus依賴的java傳送http請求元件-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.13</version>
</dependency>
<!--引入stringUtils需要的依賴-->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!--引入mysql驅動-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<!--引入http servlet包依賴,tomcat一般都有,所有這裡設定為scope=provided,打包時候不需要-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!--引入ali-Nacos服務發現中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<!--<version>2.2.0.RELEASE</version>-->
</dependency>
<!--引入ali-Nacos服務配置中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--引入阿里雲封裝好的cloud oss-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alicloud-oss</artifactId>
</dependency>
</dependencies>
<!--引入spring cloud alibaba-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<!--<version>2.2.3.RELEASE</version>-->
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--解決Cannot resolve com.alibaba.cloud:aliyun-oss-spring-boot-starter:unknown-->
<!--<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>aliyun-oss-spring-boot-starter</artifactId>
<version>1.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>-->
</dependencies>
</dependencyManagement>
</project>
5.Product.pom繼續使用之前的oss,可省略,只為測試原生上傳
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<!--<version>2.1.8.RELEASE</version>-->
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cevent.yameng.web.mall</groupId>
<artifactId>yameng-mall-product</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>yameng-mall-product</name>
<description>亞盟商城-商品服務模組</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<!--新增common公共模組依賴-->
<dependency>
<groupId>com.cevent.yameng.web.mall</groupId>
<artifactId>yameng-mall-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--引入阿里雲物件儲存,後期用alibaba cloud oss 可省略-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
6. Product.yml配置阿里雲access-key、secret-key、endpoint
7.yml配置詳情
spring:
datasource:
username: root
password: cevent
url: jdbc:mysql://***:3306/***?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.jdbc.Driver
#4.配置nacos註冊中心
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
#5.配置阿里巴巴-cloud-oss,這裡對應common.pom的spring-cloud-alibaba-dependencies(2.1.0.RELEASE),不相容
alicloud:
access-key: ***
secret-key: ***
oss:
endpoint: ***
##1.mapper-locations預設值: private String[] mapperLocations = new String[]{"classpath*:/mapper/**/*.xml"};
#classpath*:掃描包括本product系統及各個引用包下的類路徑
#classpath:只掃描本product系統
mybatis-plus:
mapper-locations: classpath*:/mapper/**/*.xml
#2.調整entity實體類的tableId為自增主鍵,資料量大後,進行分庫分表的主鍵生成規則/自定義生成規則
global-config:
db-config:
id-type: auto
# mybatis全域性邏輯刪除:1代表刪除 0代表沒有刪除
logic-delete-value: 1
logic-not-delete-value: 0
#3.配置啟動埠
server:
port: 6622
#4.設定列印日誌
logging:
level:
com.cevent.yameng.web.mall: debug
#5.配置阿里巴巴-cloud-oss,這裡是新版本aliyun的配置,不相容
#alibaba:
# cloud:
# access-key: ***
# secret-key: ***
# oss:
# endpoint: ***
8.test測試
package com.cevent.yameng.web.mall.product;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cevent.yameng.web.mall.product.entity.BrandEntity;
import com.cevent.yameng.web.mall.product.service.BrandService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
/**
* 測試brand-service
*/
@SpringBootTest
class YamengMallProductApplicationTests {
@Autowired
BrandService brandService;
//1.測試新增品牌
@Test
void contextLoads() {
BrandEntity brandEntity = new BrandEntity();
brandEntity.setName("亞盟");
brandService.save(brandEntity);
System.out.println("儲存的brand-entity:" + brandEntity.getName());
}
//2.修改品牌
@Test
void updateBrand() {
BrandEntity brandEntity = new BrandEntity();
//id為long
brandEntity.setBrandId(1L);
brandEntity.setDescript("亞盟電子商務有限公司");
brandService.updateById(brandEntity);
System.out.println("更新的brand-entity:" + brandEntity.getDescript());
}
//3.查詢品牌
@Test
void selectBrand() {
List<BrandEntity> brandEntityList;
//包裝查詢wrapper,根據指定id查詢集合
brandEntityList = brandService.list(new QueryWrapper<BrandEntity>().eq("brand_id", 1L));
//遍歷集合
brandEntityList.forEach((item) -> {
System.out.println("查詢的brand-entity:" + item);
});
}
//4.oss檔案上傳測試,這裡與OSSClient衝突
@Test
void testUpload() throws FileNotFoundException {
// Endpoint以杭州為例,其它Region請按實際情況填寫。
String endpoint = "endpoint";
// 雲賬號AccessKey有所有API訪問許可權,建議遵循阿里雲安全最佳實踐,
// 建立並使用RAM子賬號進行API訪問或日常運維,請登入 https://ram.console.aliyun.com 建立。
String accessKeyId = "ak";
String accessKeySecret = "sk";
// 建立OSSClient例項。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 上傳檔案流
//1.指定檔案
InputStream inputStream = new FileInputStream("D:\\CEVENT_INFO\\1_marknelson_anotherplace_t.jpg");
//2.上傳到bucket
ossClient.putObject("cevent-yameng-shop", "cevent_icon.jpg", inputStream);
// 關閉OSSClient。
ossClient.shutdown();
System.out.println("OSS上傳完成!");
}
@Autowired
OSSClient ossClient;
//5.alibaba-cloud-oss檔案上傳測試
@Test
void testCloudOSSUpload() throws FileNotFoundException {
// 上傳檔案流
//1.指定檔案
InputStream inputStream = new FileInputStream("D:\\CEVENT_INFO\\icon1.jpg");
//2.上傳到bucket
ossClient.putObject("cevent-yameng-shop", "cevent_red2.jpg", inputStream);
// 關閉OSSClient。
ossClient.shutdown();
System.out.println("aliyun-OSS上傳完成!");
}
}
相關文章
- Hadoop hdfs上傳檔案報錯解決Hadoop
- Could not resolve host: 'localhost 報錯解決辦法localhost
- 解決String cannot be cast to java.util.List報錯ASTJava
- asp.net 解決檔案上傳大小的限制ASP.NET
- dcat-admin 大檔案上傳(前端直傳解決)前端
- 大檔案傳輸解決方案:分片上傳 / 下載限速
- Zuul上傳檔案,中文檔名亂碼解決辦法Zuul
- Java大檔案上傳、分片上傳、多檔案上傳、斷點續傳、上傳檔案minio、分片上傳minio等解決方案Java斷點
- ASP中多檔案同時上傳解決方案 (轉)
- Android Studio 出現“Cannot resolve symbol” 解決辦法AndroidSymbol
- 關於Cannot resolve scoped service from root provider解決方案IDE
- Ubuntu Cannot allocate memory 錯誤解決方案Ubuntu
- mybatis報錯解決MyBatis
- 報錯:net::err_unknown_url_scheme的解決辦法Scheme
- JAVA web ServletFileUpload檔案上傳遇到大量50+錯誤的解決方法JavaWebServlet
- 使用pillow開啟TIFF檔案報tempfile.tif: Cannot read TIFF header錯誤的解決方案Header
- 記錄:yum Could not resolve host: mirrors.cloud.aliyuncs.com; Unknown error解決CloudError
- 解決fitz模組報錯
- SS報錯的解決
- 解決eslint報錯EsLint
- iText中文,報錯解決
- .net上傳大型視訊檔案到伺服器,解決方案伺服器
- 過濾器解決檔案上傳下載跨域問題過濾器跨域
- wordpress網站維護教程:不能上傳檔案,資料庫報錯的解決方法網站資料庫
- nginx FastCGI錯誤Primary script unknown解決辦法NginxAST
- standby新增檔案錯誤的解決方法
- vue 父子元件傳值報錯:this.$emit is not a function 解決Vue元件MITFunction
- eclipse 引入外部js檔案報Syntax error on token 錯解決方案EclipseJSError
- 關於azkaban上傳job壓縮包報錯問題的解決方案
- struts檔案上傳詳解
- 如何分發大檔案、大檔案傳輸解決方案
- cenots7.6系統報“fork:Cannot allocate memory” 報錯的解決方法(實操)
- 解決 Cannot find OpenSSL's
- 快速解決崩潰服務上傳原生符號檔案失敗符號
- 上傳檔案(圖片)失敗 error=6 找不到臨時檔案解決辦法Error
- Android Studio 顯示Cannot resolve symbol 'XXX'的解決方式AndroidSymbol
- hive使用報錯解決方法Hive
- npm install 報錯解決NPM