溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么讓Spring Rest接口中路徑參數(shù)可選

發(fā)布時(shí)間:2021-11-01 09:16:28 來(lái)源:億速云 閱讀:231 作者:小新 欄目:開(kāi)發(fā)技術(shù)

小編給大家分享一下怎么讓Spring Rest 接口中路徑參數(shù)可選,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

Spring Rest接口路徑參數(shù)可選

我有一個(gè) Spring Rest 服務(wù),其中有一個(gè)路徑參數(shù)是可選的(實(shí)際情況是我原來(lái)將參數(shù)放到路徑中,而另外一個(gè)前端通過(guò) body 傳給我)。按照傳統(tǒng)的方式是把這個(gè)服務(wù)在代碼里面分成兩個(gè)方法,一個(gè)帶路徑參數(shù),一個(gè)不帶,但是這樣看起來(lái)不優(yōu)雅,讓人疑惑。

我試著給 @PathVariable 注解加上 require=false 注解,但是不起作用,返回404錯(cuò)誤。

下面的形式就是傳統(tǒng)方式:

@RequestMapping(value = "/path/{id}")
public String getResource(@PathVariable String id) {
  return service.processResource(id);
} 
@RequestMapping(value = "/path")
public String getResource() {
  return service.processResource();
}

但是我真的不喜歡這種方式,臃腫。

從 Spring 4 and Java 8 開(kāi)始(其實(shí)和 Java 8 關(guān)系不大),在一個(gè)方法里面實(shí)現(xiàn)可選路徑參數(shù)變得很簡(jiǎn)單,如下所示,就是同時(shí)定義兩個(gè)路由:

@RequestMapping(value = {"/path", "/path/{id}")
public String getResource(@PathVariable Optional<String> id) {
  if (id.isPresent()) {
    return service.processResource(id.get());
  } else {
    return service.processResource();
  }
}

確實(shí),在一個(gè)方法里面統(tǒng)一業(yè)務(wù)要優(yōu)雅得多。

RestFul風(fēng)格傳參

RestFul風(fēng)格就是所有參數(shù)都由/傳遞,而不是傳統(tǒng)的?xx&xx形式

例如:寫(xiě)一個(gè)Controller:

package controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RestfulController {
@RequestMapping("/add")
public String test(int a,int b, Model model){
int res = a+b;
model.addAttribute("msg","結(jié)果為"+res);
return "test";
}
}

可以看到出現(xiàn)a,b沒(méi)找到的錯(cuò)誤

怎么讓Spring Rest接口中路徑參數(shù)可選

按照傳統(tǒng)方式傳遞參數(shù):?a=1&b=2

怎么讓Spring Rest接口中路徑參數(shù)可選

那么按照Restful風(fēng)格傳遞參數(shù)就應(yīng)該:在方法參數(shù)值前加@PathVariable注解,并在url上直接綁定參數(shù),可以同時(shí)設(shè)置Request的方法類型(GET、POST、DELETE、OPTIONS、HEAD、PATCH、PUT、TRACE)

@PathVariable:讓方法參數(shù)的值對(duì)應(yīng)綁定到一個(gè)url模板變量上

package controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RestfulController {
@RequestMapping(value = "/add/{a}/",method = RequestMethod.GET)
public String test(@PathVariable int a,@PathVariable int b, Model model){
int res = a+b;
model.addAttribute("msg","結(jié)果為"+res);
return "test";
}
}

再次開(kāi)啟Tomcat,并設(shè)定a=1,b=3:

/add/1/3傳遞參數(shù)

怎么讓Spring Rest接口中路徑參數(shù)可選

這就是restful風(fēng)格傳參

也可以通過(guò)變相的組合注解實(shí)現(xiàn):

  • @PostMapping

  • @GetMapping

  • @PutMapping

  • @DeleteMapping

  • @PatchMapping

看完了這篇文章,相信你對(duì)“怎么讓Spring Rest 接口中路徑參數(shù)可選”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI