package com.imooc.springbootlearn; import org.springframework.web.bind.annotation.*; /** * 演示各种传参形式 */ @RestController public class ParaController { // 此类是一个controller // 同时,具有restful风格 /** * 直接返回一个String * 示例:http://127.0.0.1:8080/firstRequest * @return */ @GetMapping({"/firstRequest"}) public String firstRequest() { return "第一个springboot接口"; } /** * 传参,函数返回含参数的string * 示例: http://127.0.0.1:8080/requestPara?num=3 * @param num * @return */ @GetMapping({"/requestPara"}) public String requestPara(@RequestParam Integer num) { return "para from request: " + num; } /** * 传参,通过url路径 * 示例: http://127.0.0.1:8080/pathPara/9 * @param num * @return */ @GetMapping({"/pathPara/{num}"}) public String pathPara(@PathVariable Integer num) { return "path from path: " + num; } /** * 多url访问、传参 * 示例: http://127.0.0.1:8080/multiUrl1?num=6 * 示例: http://127.0.0.1:8080/multiUrl2?num=6 * @param num * @return */ @GetMapping({"/multiUrl1", "/multiUrl2"}) public String multiUrl(@RequestParam Integer num) { return "para from path: " + num; } /** * 传参设置默认值,增强代码健壮性 * 示例: http://127.0.0.1:8080/required (返回参数是0的情况) * 示例: http://127.0.0.1:8080/required?num=9 (按参数值情况返回) * @param num * @return */ @GetMapping({"/required"}) public String required(@RequestParam(required = false, defaultValue = "0") Integer num) { return "para from request: " + num; } }