1、简单参数的接受
package cn.ssdt.tim; import javax.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class RequestController { // 方式一 原始的请求参数 @RequestMapping("/simpleParam") public String simpleParam(HttpServletRequest request) { String name = request.getParameter("name"); String ageStr = request.getParameter("age"); int age = Integer.parseInt(ageStr); System.out.println(name); System.out.print(age); return "ok"; } // 方式二 springboot 请求参数 @RequestMapping("/simpleParam2") public String simpleParam2(String name,Integer age) { System.out.println(name + ":"+age); return "ok"; } // post 请求 @RequestMapping("/simpleParam3") public String postSimpleParam(String name,Integer age) { System.out.println(name + ":"+age); return "ok"; } // @RequestParam 中的required属性默认true 代表该参数必须传递。若需要可选,可设置为false @RequestMapping("/simpleParam4") public String postSimpleParam2(@RequestParam(name="name",required=false)String username,Integer age) { System.out.println(username + ":"+age); return "ok"; } }
标签:name,age,System,参数,接受,out,public,String From: https://www.cnblogs.com/Mengchangxin/p/18301769