优秀的编程知识分享平台

网站首页 > 技术文章 正文

关于远程调用feign的优雅写法(feign远程调用怎么用restful)

nanyue 2025-07-28 19:27:48 技术文章 3 ℃

常规写法

@FeignClient(name = "middle-platform", contextId = "ai")
public interface AiAccountApiClient {

   /**
    * 扣减库存
    * @return Result<SendMessagesResponse>
    */
   @PostMapping("/v1/ai/aiAccountFrequency/deduction")
   Result<Long> deduction(@RequestBody @Valid AiAccountDeductionRequest request);

}

优雅写法

避免每次都需要判断请求状态码

package com.zhijian.staff.app.commons.client;

import com.zhijian.base.common.model.dto.ZBizException;
import com.zhijian.base.common.model.response.Result;
import com.zhijian.staff.app.commons.client.request.ai.AiAccountDeductionRequest;
import com.zhijian.staff.app.commons.enums.AiApplicationBizCodeEnum;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;
import java.util.List;

/**
* 短信client
 *
 * @author zhao
*/
@FeignClient(name = "middle-platform", contextId = "ai")
public interface AiAccountApiClient {

   /**
    * 扣减库存
    * @return Result<SendMessagesResponse>
    */
   @PostMapping("/v1/ai/aiAccountFrequency/deduction")
   Result<Long> deduction(@RequestBody @Valid AiAccountDeductionRequest request);



   default void deductionDeafult( AiAccountDeductionRequest request){
      Result<Long> deduction = deduction(request);
      if(deduction.getStatus() != 200){
         throw new ZBizException(AiApplicationBizCodeEnum.CURRENT_USER_HAS_NO_AVAILABLE_TIMES_ERROR);
      }
   }

}
最近发表
标签列表