首页 > 其他分享> > Springcloud学习笔记34--文件存储信息新增功能(post方式提交,@RequestBody注解接收raw格式的application/json,JsonUtil工具类)
Springcloud学习笔记34--文件存储信息新增功能(post方式提交,@RequestBody注解接收raw格式的application/json,JsonUtil工具类)
作者:互联网
1.controller
利用OssDetailsDto实体类接收多个参数,同时利用@RequestBody注解接收postman测试所发的raw格式的application/json)
@Api(tags="bs_file_store") @RestController @RequestMapping("/test/bsFileStore") @Slf4j public class BsFileStoreController extends JeecgController<BsFileStore, IBsFileStoreService> { @Autowired private IBsFileStoreService bsFileStoreService;/** * 添加 * * @param ossDetailsDto * @return */ @AutoLog(value = "bs_file_store-添加") @ApiOperation(value="bs_file_store-添加", notes="bs_file_store-添加") @PostMapping(value = "/add") public Result<?> add(@RequestBody OssDetailsDto ossDetailsDto) { return Result.OK(bsFileStoreService.add(ossDetailsDto)?Result.OK("添加成功"):Result.error("添加失败!")); }
}
2.service接口及其实现类
(1)service接口
public interface IBsFileStoreService extends IService<BsFileStore> {/** * 新建存储 * @param ossDetailsDto * @return */ boolean add(OssDetailsDto ossDetailsDto); }
(2)实现类
@Service @DS("multi-datasource1") @Slf4j public class BsFileStoreServiceImpl extends ServiceImpl<BsFileStoreMapper, BsFileStore> implements IBsFileStoreService { @Autowired private BsFileStoreMapper bsFileStoreMapper; @Autowired private PmOsInfoMapper pmOsInfoMapper; @Autowired private PmLocalstoreInfoMapper pmLocalstoreInfoMapper; @Autowired private PmOsClustersMapper pmOsClustersMapper; @Autowired private PmOsBucketMapper pmOsBucketMapper; private final String LOCAL=StoreTypeEnum.LOCAL.getCode(); private final String OSS=StoreTypeEnum.OSS.getCode(); @Override @Transactional(rollbackFor = Exception.class) //数据库事务操作 public boolean add(OssDetailsDto ossDetailsDto) { String type = ossDetailsDto.getStoreType(); String osName = ossDetailsDto.getOsName(); if(OSS.equals(type)){ PmOsInfo osInfo=new PmOsInfo(); BeanUtils.copyProperties(ossDetailsDto,osInfo ); pmOsInfoMapper.insert(osInfo); //集群关系插入 ossDetailsDto.getClusters().forEach(i->{ i.setOsName(osName); pmOsClustersMapper.insert(i); }) ; } return true; } }
3.postman测试
application/json格式的测试数据生成
JsonUtil工具类:
package org.jeecg.modules.util; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.jeecg.modules.dto.OssDetailsDto; import org.jeecg.modules.entity.PmOsClusters; import java.io.IOException; import java.util.ArrayList; import java.util.List; @Slf4j public class JsonUtil { private static ObjectMapper objectMapper=new ObjectMapper(); static { objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false); } public static <T> String obj2JsonStr(T t) throws JsonProcessingException { if(t instanceof String ){ return (String) t; } return objectMapper.writeValueAsString(t); } public static <T> T jsonStr2Obj(String str,Class<T> clazz) throws IOException { if(str==null){ return null; } //若反序列化类型为String,则直接返回 if(String.class.getTypeName().equals(clazz.getTypeName())) { return (T) str; } return (T) objectMapper.readValue(str, clazz); } public static <T> T jsonStr2Obj(String str,TypeReference typeReference) throws IOException{ if(str==null){ return null; } //若反序列化类型为String,则直接返回 if(String.class.getTypeName().equals(typeReference.getType().getTypeName())) { return (T) str; } return (T) objectMapper.readValue(str, typeReference); } public static String formatJsonStr(Object o) throws IOException{ //判断是否为空 if(o==null){ return null; } Object tmp=o; //若输入参数为String类型,则尝试转换为Object,转换失败则直接返回 if(tmp instanceof String){ String tmpStr=(String) tmp; if(tmpStr.length()==0){ return tmpStr; } try { tmp=jsonStr2Obj(tmpStr, Object.class); } catch (JsonProcessingException e) { log.warn("反序列化失败,无法反序列化[{}]",tmpStr); return tmpStr; } } try { return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(tmp); } catch (JsonProcessingException e) { //若转格式化失败,则返回toString log.warn("",e); return tmp.toString(); } } public static String jsonGenerate() throws JsonProcessingException { OssDetailsDto ossDetailsDto=new OssDetailsDto(); ossDetailsDto.setStoreType("1"); ossDetailsDto.setOsDesc("hello"); ossDetailsDto.setOsName("fleptest1014"); ossDetailsDto.setLocalPath("d:/test"); ossDetailsDto.setEndpointFirst("l27.0.0.1"); ossDetailsDto.setAccessKey("aaa"); ossDetailsDto.setSecretKey("sss"); ossDetailsDto.setStatus("1"); List<PmOsClusters> osClustersList=new ArrayList<>(); PmOsClusters pmOsClusters=new PmOsClusters(); pmOsClusters.setOsName("lucky01"); pmOsClusters.setStatus("1"); osClustersList.add(pmOsClusters); ossDetailsDto.setClusters(osClustersList); String s = obj2JsonStr(ossDetailsDto); return s; } public static void main(String[] args) throws Exception { System.out.println(jsonGenerate()); } }
测试json实例:
{"id":null,"osName":"fleptest1014","osDesc":"hello","storeType":"1","endpointFirst":"l27.0.0.1","endpointSecond":null,"endpointThird":null,"localPath":"d:/test","pathDesc":null,"accessKey":"aaa","secretKey":"sss","status":"1","clusters":[{"id":null,"osName":"lucky01","clusterName":null,"status":"1","createBy":null,"createTime":null,"updateBy":null,"updateTime":null}],"buckets":null}
postman测试界面:
url:http://127.0.0.1:7009/test/bsFileStore/add
数据库表查看截图:
标签:return,String,ossDetailsDto,JsonUtil,RequestBody,raw,import,null,public 来源: https://www.cnblogs.com/luckyplj/p/15408210.html