Spring Boot路由參數配置

Spring Boot

  • package com.tang.demo1.controller;
  • import org.springframework.web.bind.annotation.*;
  • @RestController
  • public class RouterController {
@RequestMapping(path = {"/getJson/{name}/{id}"})
@ResponseBody
public Map<String,String> getJson(@PathVariable("name") String name,@PathVariable("id") Integer id,@RequestParam(value = "type",defaultValue = "news")String type,@RequestParam(value = "num",required = false) int num){
    Map<String,String> rMap = new HashMap<String, String>();
    rMap.put("name", "this is article's name : "+name );     // 5
    rMap.put("id", "this is article's id : " + id);
    rMap.put("type","this is article's type :"+type);
    rMap.put("num","this is article's num :"+num);
    return rMap;
}
}

在url路徑中的參數,被稱爲pathVariable,查詢參數被稱爲pequestParm。在controller中接受參數,可以直接在方法裏用了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章