Spring Boot中路由传递参数



在路由时传递参数,一般有两种形式,一种是拼接在url地址中,另一种是查询参数。如:http://localhost:8080/router/tang/101?type=spor&num=12。下面根据代码看一下,VUE 和 Spring Boot 中各自是如何处理传递和接受参数的。

Spring Boot

  • package com.tang.demo1.controller;
  • import org.springframework.web.bind.annotation.*;
  • @RestController
  • public class RouterController {
  • @RequestMapping(path = { "/router/{name}/{classid}"}, method = RequestMethod.GET)
  • public String router(@PathVariable("name") String name
  • ,@PathVariable("classid") int classid
  • ,@RequestParam(value = "type", defaultValue = "news") String type
  • ,@RequestParam(value = "num", required = falsef) int num){
  • // 访问 http://localhost:8080/router/tang/101?type=spor&num=12
  • return name + classid + type + num;
  • }
  • }

在url路径中的参数,被称为pathVariable,查询参数被称为requestParm。在controller中接受参数,可以直接在方法里用了。

猜你喜欢

转载自blog.csdn.net/jiaowo_ccc/article/details/79639547