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> 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> rMap = new HashMap, 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中接受参数,可以直接在方法里用了。

你可能感兴趣的:(MVC,springboot,Java)