1. 程式人生 > 其它 >SpringCloud 之 Fegin跨服務呼叫

SpringCloud 之 Fegin跨服務呼叫

技術標籤:javajava

	最近在做一個微服務的專案,專案中用的Feign來做服務件呼叫,由於自己也是第一次使用,記錄一下自己遇到的坑。

啟動類加入註解:

@EnableRyFeignClients

引入依賴:

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>

客戶端介面類:

package com.dlzx.pfservercar.client;

import com.dlzx.common.core.web.domain.AjaxResult;
import com.dlzx.common.core.web.page.TableDataInfo;
import com.dlzx.pfservercar.domain.DVacation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Component
@FeignClient(value = "pfserver-person") //註冊中心服務的名字
//@FeignClient(value = "pfserver-person",url = "http://localhost:9500/") //本地除錯
public interface PersonClient {


    /**
     * App 考勤-駕駛員請假列表
     * */
    @RequestMapping(path = "/vacation/E01VacationList",method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
    public TableDataInfo E01VacationList(@RequestBody DVacation dVacation);

    /**
     * App 考勤-請假詳情(區分四種狀態)
     */
    @RequestMapping(path = "/vacation/E02VacationDetail")
    public AjaxResult E02VacationDetail(@RequestParam("id") String id);
}

消費端Controller:

/**
 * 駕駛員-考勤Controller
 * 
 * @author dcl
 * @date 2020-11-30
 */
@RestController
@RequestMapping("/driver-api/vacation")
public class DVacationController extends BaseController
{
    @Autowired
    private PersonClient personClient;

    /**
     * App 考勤-駕駛員請假列表
     */
    @RequestMapping("/E01VacationList")
    @ResponseBody
    @Log(title = "考勤-駕駛員請假列表")
    public TableDataInfo list(@RequestBody DVacation dVacation)
    {
        return personClient.E01VacationList(dVacation);
    }

服務提供端Controller:

    /**
     * App 考勤-駕駛員請假列表
    * */
    @RequestMapping(path = "/E01VacationList",method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
    @ResponseBody
    @Log(title = "App 考勤-駕駛員請假列表")
    public TableDataInfo vacationList(@RequestBody DVacation dVacation)
    {
        startPage(dVacation.getPageNum(),dVacation.getPageSize());
        LoginDriver loginDriver = driverTokenService.getLoginDriver();
        dVacation.setDriverId(loginDriver.getUserid()+"");
        return getDataTable(dVacationService.queryAppVacationList(dVacation));
    }

一開始都還好好的,當傳入多個@RequestParam引數時,突然遇到一個錯誤“Required String parameter ‘XXX’ is not present” 結果是因為我傳入的引數有一個是空的,就會導致這個報錯,如果引數可能為空的時候避免報錯,可以參考我的。
將@RequestParam(“type”) 改為
@RequestParam(value = “type”,required = false)
可以解決此問題。

/**
     * 管理員公共介面-訊息節點
     */
    @RequestMapping(path = "/administrator-app/msg/addAdminMsg",method = RequestMethod.POST)
    public AjaxResult addAdminMsg(@RequestParam(value = "type",required = false)Long type, @RequestParam(value = "relationId",required = false)String relationId,
                                  @RequestParam(value = "title",required = false)String title, @RequestParam(value = "content",required = false)String content);

這個引數是 required 確定在 API 中的引數中是否必須要輸出引數。在預設情況下為 true,你可以設定這個引數為 false。如果你設定的引數為 true,但是在提交引數的時候沒有提交這個引數,你的 API 將會返回一個異常。