第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

使用Spring,我可以創(chuàng)建一個(gè)可選的路徑變量嗎?

使用Spring,我可以創(chuàng)建一個(gè)可選的路徑變量嗎?

繁華開滿天機(jī) 2019-11-11 14:40:31
在Spring 3.0中,我可以有一個(gè)可選的path變量嗎?例如@RequestMapping(value = "/json/{type}", method = RequestMethod.GET)public @ResponseBody TestBean testAjax(        HttpServletRequest req,        @PathVariable String type,        @RequestParam("track") String track) {    return new TestBean();}在這里我想/json/abc還是/json要調(diào)用相同的方法。一種明顯的解決方法是聲明type為請(qǐng)求參數(shù):@RequestMapping(value = "/json", method = RequestMethod.GET)public @ResponseBody TestBean testAjax(        HttpServletRequest req,        @RequestParam(value = "type", required = false) String type,        @RequestParam("track") String track) {    return new TestBean();}然后/json?type=abc&track=aa或/json?track=rr將工作
查看完整描述

3 回答

?
HUWWW

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

您不能具有可選的路徑變量,但是可以有兩個(gè)調(diào)用相同服務(wù)代碼的控制器方法:


@RequestMapping(value = "/json/{type}", method = RequestMethod.GET)

public @ResponseBody TestBean typedTestBean(

        HttpServletRequest req,

        @PathVariable String type,

        @RequestParam("track") String track) {

    return getTestBean(type);

}


@RequestMapping(value = "/json", method = RequestMethod.GET)

public @ResponseBody TestBean testBean(

        HttpServletRequest req,

        @RequestParam("track") String track) {

    return getTestBean();

}


查看完整回答
反對(duì) 回復(fù) 2019-11-11
?
白板的微信

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

如果您在使用Spring 4.1和Java 8,你可以使用java.util.Optional它支持@RequestParam,@PathVariable,@RequestHeader和@MatrixVariableSpring MVC中-


@RequestMapping(value = {"/json/{type}", "/json" }, method = RequestMethod.GET)

public @ResponseBody TestBean typedTestBean(

    @PathVariable Optional<String> type,

    @RequestParam("track") String track) {      

    if (type.isPresent()) {

        //type.get() will return type value

        //corresponds to path "/json/{type}"

    } else {

        //corresponds to path "/json"

    }       

}


查看完整回答
反對(duì) 回復(fù) 2019-11-11
?
FFIVE

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

眾所周知,您還可以使用@PathVariable批注注入路徑變量的Map。我不確定該功能是否在Spring 3.0中可用或是否在以后添加,但是這是解決示例的另一種方法:


@RequestMapping(value={ "/json/{type}", "/json" }, method=RequestMethod.GET)

public @ResponseBody TestBean typedTestBean(

    @PathVariable Map<String, String> pathVariables,

    @RequestParam("track") String track) {


    if (pathVariables.containsKey("type")) {

        return new TestBean(pathVariables.get("type"));

    } else {

        return new TestBean();

    }

}


查看完整回答
反對(duì) 回復(fù) 2019-11-11
  • 3 回答
  • 0 關(guān)注
  • 547 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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