@FeignClient 调用远程服务

一、传参数

1、restful传

service调用

@RequestMapping("/act/delDeployById/{deployId}")
    public void delDeployById(@PathVariable("deployId") String deployId);

服务controller

@RequestMapping("/act/delDeployById/{deployId}")
    public void delDeployById(@PathVariable("deployId") String deployId){

2、通过参数实体传送,通过把所有参数放到Map里边

service调用

@RequestMapping("/act/queryProDefList")
    public List<Map<String, Object>> queryProDefList(@RequestBody Map<String,Object>paramMap);

服务controller

@RequestMapping("/act/queryProDefList")
    public List<Map<String, Object>> queryProDefList(@RequestBody Map<String,Object>paramMap){

    Integer startIndex = (Integer) paramMap.get("startIndex");
        Integer pageSize = (Integer) paramMap.get("pageSize");

}

3、通过restTemplate调用服务

上传文件(意思就是上传以后然后部署activiti流程)

@ResponseBody
    @RequestMapping("/upload")
    public Object upload(HttpServletRequest req){
        start();
        try {
            MultipartHttpServletRequest request = (MultipartHttpServletRequest)req;
            MultipartFile file = request.getFile("procDefFile");
            System.out.println(file.getName());
            System.out.println(file.getOriginalFilename());
            String filename = file.getOriginalFilename();
            String uuid = UUID.randomUUID().toString();
            File createTempFile = File.createTempFile(uuid, filename.substring(filename.lastIndexOf(".") ));
            file.transferTo(createTempFile);
            
            FileSystemResource  resource = new FileSystemResource(createTempFile);
            MultiValueMap<String, Object> param = new LinkedMultiValueMap<String, Object>();
            param.add("pdfile", resource);
            String postForObject = restTemplate.postForObject("http://atcrowdfunding-activiti-service/act/deploy", param,String.class);
            System.out.println("result"+postForObject);
            createTempFile.delete();
            success(true);
        } catch (Exception e) {
            e.printStackTrace();
            success(false);
        }
        
        return end();
    }

@RequestMapping("/act/deploy")
    public String deploy(@RequestParam("pdfile") MultipartFile file){
        start();
        try {
            repositoryService.createDeployment()
            .addInputStream(file.getOriginalFilename(), file.getInputStream())
            .deploy();
            return "成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "失败";
        }
        
    }

四、加载图片(把图片下载下来)

@ResponseBody
    @RequestMapping("/load")
    public void load(String pdid,HttpServletResponse resp) throws Exception{
        //通过响应头返回图形信息
        HttpHeaders httpHeader = new HttpHeaders();
        httpHeader.setContentType(MediaType.IMAGE_PNG);
        String url="http://atcrowdfunding-activiti-service/act/loadImgById/"+pdid;
        
        ResponseEntity<byte[]> reponse=restTemplate.exchange(url,HttpMethod.POST,
                new HttpEntity<byte[]>(httpHeader), byte[].class);
        byte[] result = reponse.getBody();
        
        InputStream in = new ByteArrayInputStream(result);
        
        OutputStream out = resp.getOutputStream();
        
        int i = -1;
        while((i=in.read())!=-1){
            out.write(i);
        }
    }

@RequestMapping("/act/loadImgById/{pdid}")
    public byte[] loadImgById(@PathVariable("pdid") String pdid) throws Exception{
        ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();
        ProcessDefinition singleResult = query.processDefinitionId(pdid).singleResult();
        InputStream in = repositoryService.getResourceAsStream(singleResult.getDeploymentId(), singleResult.getDiagramResourceName());
        
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buf = new byte[100];
        int rc = 0;
        while((rc=in.read(buf,0,100))>0){
            swapStream.write(buf,0,rc);
        }
        byte[] byteArray = swapStream.toByteArray();
        return byteArray;
        
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Spring Cloud中,可以使用@FeignClient注解来调用远程服务接口。@FeignClient注解是一个声明式的Web服务客户端,可以将一个服务接口定义成Java接口,然后使用注解的方式来调用远程服务。 具体步骤如下: 1. 引入Feign依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 2. 创建服务接口 创建一个Java接口,用于定义远程服务的接口方法。例如: ``` @FeignClient(name = "remote-service") public interface RemoteService { @GetMapping("/hello") String sayHello(); } ``` @FeignClient注解中的name属性指定了远程服务的名称,这个名称对应了服务注册中心中的服务名。 3. 调用远程服务 在需要调用远程服务的地方,通过@Autowired注入RemoteService接口实例,然后直接调用接口中的方法即可。 ``` @RestController public class MyController { @Autowired private RemoteService remoteService; @GetMapping("/test") public String test() { return remoteService.sayHello(); } } ``` 在上面的例子中,MyController通过调用RemoteService接口中的sayHello()方法来调用远程服务中的/hello接口。 需要注意的是,@FeignClient注解默认使用的是Spring MVC注解,因此在定义服务接口方法时需要使用Spring MVC的注解来指定请求方式、请求路径等信息。例如,在RemoteService接口中的sayHello()方法上使用@GetMapping注解来指定使用GET请求访问/hello接口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值