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上传完成!");
}
}