最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

將 2 個(gè)控制器方法映射到具有不同查詢參數(shù)的同一端點(diǎn)

將 2 個(gè)控制器方法映射到具有不同查詢參數(shù)的同一端點(diǎn)

至尊寶的傳說(shuō) 2024-01-17 20:51:50
我想在 Java 中實(shí)現(xiàn)一個(gè)帶有“重載”端點(diǎn)的 REST API,該端點(diǎn)因傳遞的查詢參數(shù)而異。我在我的控制器類中嘗試過(guò)這段代碼:@Get("/query")public MyResponse queryByDate(@QueryValue @Valid @Format("yyyy-MM-dd") Date date) {    // Code to generate the response    return retval;}@Get("/query")public MyResponse queryByDateAndValue(@QueryValue @Valid @Format("yyyy-MM-dd") Date date, @QueryValue int value) {    // Code to generate the response    return retval;}這將返回以下錯(cuò)誤:More than 1 route matched the incoming request. The following routes matched /calendar/years/query: GET - /calendar/years/query, GET - /calendar/years/queryio.micronaut.web.router.exceptions.DuplicateRouteException: More than 1 route matched the incoming request. The following routes matched /calendar/years/query: GET - /calendar/years/query, GET - /calendar/years/query請(qǐng)注意,如果我刪除其中一種方法,其余方法將按預(yù)期工作。如何將具有不同查詢參數(shù)的端點(diǎn)映射到控制器中的 2 個(gè)不同方法?這可能嗎?
查看完整描述

1 回答

?
倚天杖

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

您收到錯(cuò)誤是因?yàn)閮蓚€(gè)端點(diǎn)具有相同的路徑,并且它會(huì)混淆編譯器將 API 映射到相應(yīng)的路徑。傳遞查詢參數(shù)并不意味著端點(diǎn)具有不同的路徑。您可以將這兩個(gè)端點(diǎn)合二為一:


@Get("/query")

public MyResponse queryByDateAndValue(@QueryValue @Valid @Format("yyyy-MM-dd") Date date,@DefaultValue("1")  @QueryValue int value) {

    // Code to generate the response

    return retval;

}

并設(shè)置默認(rèn)值value。然后您可以相應(yīng)地分開(kāi)您的案例。


查看完整回答
反對(duì) 回復(fù) 2024-01-17
  • 1 回答
  • 0 關(guān)注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)