Spring Boot
- package com.tang.demo1.controller;
- import org.springframework.web.bind.annotation.*;
- 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中接受參數,可以直接在方法裏用了。