SpringBoot Web 开发请求参数
web__3">简单的 web 请求:
java">@RestController
public class HelloController {@RequestMapping("sayHello")public String sayHello(){System.out.println("Hello World");return "hello world";}
}
获取请求参数
简单参数
- 原始方式
HttpServletRequest
:
java">@RestController
public class HelloController {@RequestMapping("sayHello")public String sayHello(HttpServletRequest request){String name = request.getParameter("name");System.out.println(name);return "OK";}
}
- SpringBoot 方式:
java">@RestController
public class HelloController {@RequestMapping("sayHello")public String sayHello(String name){System.out.println(name);return "OK";}
}
使用@RequestParam
映射,解决方法形参与请求参数名称不匹配:
java">@RequestMapping("sayHello")
public