SpringcloudrestTemplate传递复杂参数的⽅式(多个对象)使⽤微服务的时候往往服务之间调⽤⽐较⿇烦,spring cloud提供了Feign接⼝调⽤,RestTemplate调⽤的⽅式
这⾥我探讨下RestTemplate调⽤的⽅式:
服务A:接收三个对象参数这三个参数的是通过数据库查询出来的
服务B:要调⽤服务A 服务B提供了查询三个参数的⽅法,后⾯要使⽤三个参数
对于服务A,处理的⽅式有两中
1. 服务B提供⼀个Feign接⼝将查询三个参数的⽅法公开,服务A直接引⽤Feign来查询参数,服务B只需要将三个查询关键字传递过去即可
服务A action
@PostMapping("/import/{busiCode}/{filePath}")
public Map<String,String> importExcel(@PathVariable("filePath") String filePath,@PathVariable("busiCode") String busiCode,@RequestBody Map<String, String> params,                    HttpServletRequest request,HttpServletResponse response) {
response.setCharacterEncoding("UTF-8");
UserInfo user = User();
return excelService.importExcel(filePath,busiCode,params,user);
}
服务A service
//引⼊Feign接⼝
private ExcelFreign excelFreign;
public Map<String,String> importExcel(String filePath, String busiCode,Map<String, String> params,UserInfo user ) {
Map<String,String> result=new HashMap<String,String>();
excelFreign = ApplicationContext().getBean(ExcelFreign.class);
CmdImportConfigDto configDto = CmdImportConfigByBusiCode(busiCode);
CmdImportDto importDto=new CmdImportDto();
importDto.Id());
importDto.setExcelPath(filePath);
importDto.setParam(new GsonBuilder().create().toJson(params));
importDto.setLog("");
Long impId=null;
try {
impId= Long.valueOf(excelFreign.saveCmdImportDto(importDto));
} catch (Exception e1) {
e1.printStackTrace();
result.put("error", "保存出现异常");
result.put("message", e1.getMessage());
return result;
}
try{
excelFreign.updateImportStatus(impId, ImportConstant.ImportStatus.SUBMIT, "提交成功");
}catch(Exception e){
e.printStackTrace();
}
ValidateTask validateTask=new ValidateTask();
validateTask.init(impId,filePath, busiCode, params,user);
String message;
try {
message = validateTask.call();
} catch (Exception e) {
e.printStackTrace();
result.put("error", "验证出现异常");
result.put("message", e.getMessage());
return result;
}
if(message!=null){
result.put("error", "验证不通过");
result.put("message", message);
return result;
}
PersistTask persistTask=new PersistTask();
persistTask.init(impId,filePath, busiCode, params,user);
result.putAll(ImportQueue.submit(persistTask));
return result;
}
服务B 提供的B-Fegin
@FeignClient(value = "frame-service",path = "/excelApi/v1")
public interface ExcelFreign extends ExcelApi {
}
服务B api层 B-api
public interface ExcelApi {
/**
* 更新状态
* @param impId
* @param importType
* @param result
*/
@PostMapping("/updateImportStatus/{impId}/{importType}/{result}")
void updateImportStatus(@PathVariable("impId") Long impId, @PathVariable("importType") String importType, @PathVariable("result") String result) throws Exception; /**
* 获取导⼊配置项
* @param busiCode
* @return
*/
@GetMapping("/getImportConfig/{busicode}")
CmdImportConfigDto getCmdImportConfigByBusiCode(@PathVariable("busicode") String busiCode);
/**
* 保存信息
* @param importDto
* @return
*/
@PostMapping("/saveImport")
String saveCmdImportDto(@RequestBody CmdImportDto importDto);
}
服务B 实现api接⼝的action
@RestController
@RequestMapping("/excelApi/v1")
public class ExcelFeignAction implements ExcelApi {
@Autowired
private CmdExportService exportService;
/**
* 获取导⼊配置项
* @param busiCode
* @return
*/
@GetMapping("/getImportConfig/{busicode}")
public CmdImportConfigDto getCmdImportConfigByBusiCode(@PathVariable("busicode") String busiCode){
CmdImportConfigByBusiCode(busiCode);
}
/**
* 更新状态
* @param impId
* @param importStatus
* @param result
*/
@PostMapping("/updateImportStatus/{impId}/{importType}/{result}")
public void updateImportStatus(@PathVariable("impId") Long impId, @PathVariable("importType") String importStatus, @PathVariable("result") String result) throws Exception{
cmdImportService.updateImportStatus(impId,importStatus,new Date() , result);
}
/**
* 保存信息
* @param importDto
* @return
*/
@PostMapping("/saveImport")
public String saveCmdImportDto(@RequestBody CmdImportDto importDto){
try{
cmdImportService.saveCmdImportDto(importDto);
Id();
}catch (Exception e){
e.printStackTrace();
throw new BusinessRuntimeException("系统出现异常");
}
}
}
服务B 调⽤服务A  action层
/
**
*
* @param busicode 导出的业务编码能确定某个模块做导出操作
* @param values 请求参数
*
*        通过restTemplate 传递复杂参数
* @return
* 返回⽂件流让浏览器弹出下载
*/
@PostMapping(value = "/export/v3/{busicode}")
@ResponseBody
public ResponseEntity<byte[]> expDownLoadV3(@PathVariable("busicode") String busicode , @RequestBody Map<String,Object> values, HttpServletRequest request)throws Exception {    if(StringUtils.isBlank(busicode)){
throw new BusinessRuntimeException("参数错误,请检查参数是否正确,busicode ?");
}
// 获取执⾏过程
Map map = restTemplate.postForObject("" + serviceId + "/excelApi/v1/⽂件名"/"+busicode,values,Map.class);
String path = (("filepath");
byte[] excel = FastDFSClient.downloadToBytes(path);
CmdExportConfigDto cmdExportConfig = CmdExportConfigByBusiCode(busicode);
//获取⽂件名
String fileName = ReportName();
// 获取⽂件后缀名
String extFileName = path.substring(path.lastIndexOf('.')+1);
HttpHeaders headers = new HttpHeaders();
// 获取⽤户浏览器的种类对不同的浏览器进⾏编码处理
final String userAgent = Header("USER-AGENT");
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", ansFromFileName(userAgent,fileName) + "." + extFileName);
return new ResponseEntity<byte[]>(excel,headers,HttpStatus.OK);
}
2.服务B将查询出来的参数直接传递给服务A
服务A:
/**
* 接收参数传递
* 分别接收下⾯三种key value的键值对
* cmdExportConfig:CmdExportConfigDto
* exportFieldList:List<CmdExportFieldConfigDto>
* params:Map
* @param params
* @param request
* @param response
* @return
*/
@PostMapping("/export/v2")
public ResponseEntity exportExcel(@RequestBody Map<String,Object> params,HttpServletRequest request,HttpServletResponse response) {
response.setCharacterEncoding("UTF-8");
try {
// 将⽂件的路径获取到
ObjectMapper mapper = new ObjectMapper();
LinkedHashMap requestParMap = (("cmdExportConfig");
CmdExportConfigDto cmdExportConfigDto = null;
List<CmdExportFieldConfigDto> exportFieldList = null;
if(requestParMap.size()>0){
cmdExportConfigDto = vertValue(requestParMap,CmdExportConfigDto.class);
}
ArrayList arrayList = (("exportFieldList");
if(arrayList.size()>0){
exportFieldList = vertValue(arrayList, new TypeReference<CmdExportFieldConfigDto>() {});
}
Map values = (("params");
String filePath = portExcel(cmdExportConfigDto,exportFieldList,ServletContext().getRealPath("/"));
Map<String,String> map = new HashMap<String, String>();
map.put("filepath", filePath);
return new ResponseEntity(map,HttpStatus.OK);
}catch (IOException e){
throw new RuntimeException("输出⽂件出错");
}
}
服务B:
/**
*
* @param busicode 导出的业务编码能确定某个模块做导出操作
* @param values 请求参数
*
*        通过restTemplate 传递复杂参数
* @return
* 返回⽂件流让浏览器弹出下载⽬前需要解决将字节流响应到浏览器的控制台了后⾯均采⽤url下载的⽅式
*/
@PostMapping(value = "/export/v3/{busicode}",produces = MediaType.TEXT_PLAIN_VALUE)
@ResponseBody
public ResponseEntity<byte[]> expDownLoadV3(@PathVariable("busicode") String busicode , @RequestBody Map<String,Object> values, HttpServletRequest request)throws Exception {    String busiCode = ("busiCode").toString();
if(StringUtils.isBlank(busiCode)){
throw new BusinessRuntimeException("参数错误,请检查参数是否正确,busiCode ?");
}
// 获取执⾏过程
Map map = excuteRestTemplate(busiCode,values);
String path = (("filepath");
byte[] excel = FastDFSClient.downloadToBytes(path);
CmdExportConfigDto cmdExportConfig = CmdExportConfigByBusiCode(busiCode);
//获取⽂件名
String fileName = ReportName();
// 获取⽂件后缀名
String extFileName = path.substring(path.lastIndexOf('.')+1);
HttpHeaders headers = new HttpHeaders();erAgent = Header("USER-AGENT");
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", ansFromFileName(userAgent,fileName) + "." + extFileName);
return new ResponseEntity<byte[]>(excel,headers,HttpStatus.OK);
}
/**
* 执⾏请求调⽤
* @param busiCode
* @param variables
* @return
*/
private Map excuteRestTemplate(String busiCode,Map variables){
String serviceId="";
//查询导出配置
CmdExportConfigDto cmdExportConfig = CmdExportConfigByBusiCode(busiCode);
serviceId = SystemType();
if(cmdExportConfig==null){
getsavefilename
throw new BusinessRuntimeException("没有导出配置⽆法导出");
}
//根据导出配置id获取导出字段信息
List<CmdExportFieldConfigDto> exportFieldList = Id());
if(StringUtils.isBlank(serviceId)){
throw new BusinessRuntimeException("未配置导出的服务");
}
Map<String, Object> uriVariables = new HashMap<>();
uriVariables.put("cmdExportConfig",cmdExportConfig);
uriVariables.put("exportFieldList",exportFieldList);
uriVariables.put("params",variables);
return restTemplate.postForObject("" + serviceId + "/excelService/export/v2",new HttpEntity(uriVariables),Map.class);
}
设置浏览器头
/**
* 根据不同的浏览器类型设置下载⽂件的URL编码
* @param userAgent
* @param fileName
* @return
* @throws Exception
*/
public static String transFromFileName(String userAgent,String fileName) throws Exception{
String finalFileName = "";
ains(userAgent, "MSIE")){//IE浏览器
finalFileName = de(fileName,"UTF-8");
}else ains(userAgent, "Mozilla")){//google,⽕狐浏览器
finalFileName = new Bytes("GBK"), "ISO8859-1");
}else{
finalFileName = de(fileName,"UTF-8");//其他浏览器
}
return finalFileName;
}
总结
以上所述是⼩编给⼤家介绍的Spring cloud restTemplate 传递复杂参数的⽅式(多个对象),希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。