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>
<dependency>
<groupId>com.baomidou
</groupId>
<artifactId>mybatis-plus-boot-starter
</artifactId>
<version>3.3.1
</version>
</dependency>
<dependency>
<groupId>org.projectlombok
</groupId>
<artifactId>lombok
</artifactId>
<version>1.18.8
</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents
</groupId>
<artifactId>httpcore
</artifactId>
<version>4.4.13
</version>
</dependency>
<dependency>
<groupId>commons-lang
</groupId>
<artifactId>commons-lang
</artifactId>
<version>2.6
</version>
</dependency>
<dependency>
<groupId>mysql
</groupId>
<artifactId>mysql-connector-java
</artifactId>
<version>8.0.17
</version>
</dependency>
<dependency>
<groupId>javax.servlet
</groupId>
<artifactId>servlet-api
</artifactId>
<version>2.5
</version>
<scope>provided
</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud
</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery
</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud
</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config
</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud
</groupId>
<artifactId>spring-cloud-starter-alicloud-oss
</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud
</groupId>
<artifactId>spring-cloud-alibaba-dependencies
</artifactId>
<version>2.1.0.RELEASE
</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>
<relativePath/>
</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>
<dependency>
<groupId>com.cevent.yameng.web.mall
</groupId>
<artifactId>yameng-mall-common
</artifactId>
<version>0.0.1-SNAPSHOT
</version>
</dependency>
<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
cloud:
nacos:
discovery:
server-addr: 127.0.0.1
:8848
alicloud:
access-key: ***
secret-key: ***
oss:
endpoint: ***
mybatis-plus:
mapper-locations: classpath*
:/mapper/**/*.xml
global-config:
db-config:
id-type: auto
logic-delete-value: 1
logic-not-delete-value: 0
server:
port: 6622
logging:
level:
com.cevent.yameng.web.mall: debug
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
;
@SpringBootTest
class YamengMallProductApplicationTests {
@Autowired
BrandService brandService
;
@Test
void contextLoads() {
BrandEntity brandEntity
= new BrandEntity();
brandEntity
.setName("亚盟");
brandService
.save(brandEntity
);
System
.out
.println("保存的brand-entity:" + brandEntity
.getName());
}
@Test
void updateBrand() {
BrandEntity brandEntity
= new BrandEntity();
brandEntity
.setBrandId(1L
);
brandEntity
.setDescript("亚盟电子商务有限公司");
brandService
.updateById(brandEntity
);
System
.out
.println("更新的brand-entity:" + brandEntity
.getDescript());
}
@Test
void selectBrand() {
List
<BrandEntity> brandEntityList
;
brandEntityList
= brandService
.list(new QueryWrapper<BrandEntity>().eq("brand_id", 1L
));
brandEntityList
.forEach((item
) -> {
System
.out
.println("查询的brand-entity:" + item
);
});
}
@Test
void testUpload() throws FileNotFoundException
{
String endpoint
= "endpoint";
String accessKeyId
= "ak";
String accessKeySecret
= "sk";
OSS ossClient
= new OSSClientBuilder().build(endpoint
, accessKeyId
, accessKeySecret
);
InputStream inputStream
= new FileInputStream("D:\\CEVENT_INFO\\1_marknelson_anotherplace_t.jpg");
ossClient
.putObject("cevent-yameng-shop", "cevent_icon.jpg", inputStream
);
ossClient
.shutdown();
System
.out
.println("OSS上传完成!");
}
@Autowired
OSSClient ossClient
;
@Test
void testCloudOSSUpload() throws FileNotFoundException
{
InputStream inputStream
= new FileInputStream("D:\\CEVENT_INFO\\icon1.jpg");
ossClient
.putObject("cevent-yameng-shop", "cevent_red2.jpg", inputStream
);
ossClient
.shutdown();
System
.out
.println("aliyun-OSS上传完成!");
}
}