| 9 | import org.springframework.web.bind.annotation.RequestParam; |
| 10 | |
| 11 | @Controller |
| 12 | public class RequestParamTest { |
| 13 | @RequestMapping("/requestParam") |
| 14 | public String main(HttpServletRequest request) { |
| 15 | String year = request.getParameter("year"); |
| 16 | // http://localhost/ch2/requestParam ---->> year=null |
| 17 | // http://localhost/ch2/requestParam?year= ---->> year="" |
| 18 | // http://localhost/ch2/requestParam?year ---->> year="" |
| 19 | System.out.printf("[%s]year=[%s]%n", new Date(), year); |
| 20 | return "yoil"; |
| 21 | } |
| 22 | |
| 23 | @RequestMapping("/requestParam2") |
| 24 | // public String main2(@RequestParam(name="year", required=false) String year) { // 아래와 동일 |
| 25 | public String main2(String year) { |
| 26 | // http://localhost/ch2/requestParam2 ---->> year=null |
| 27 | // http://localhost/ch2/requestParam2?year ---->> year="" |
| 28 | System.out.printf("[%s]year=[%s]%n", new Date(), year); |
| 29 | return "yoil"; |
| 30 | } |
| 31 | |
| 32 | @RequestMapping("/requestParam3") |
| 33 | // public String main3(@RequestParam(name="year", required=true) String year) { // 아래와 동일 |
| 34 | public String main3(@RequestParam String year) { |
| 35 | // http://localhost/ch2/requestParam3 ---->> year=null 400 Bad Request. required=true라서 |
| 36 | // http://localhost/ch2/requestParam3?year ---->> year="" |
| 37 | System.out.printf("[%s]year=[%s]%n", new Date(), year); |
| 38 | return "yoil"; |
| 39 | } |
| 40 | |
| 41 | @RequestMapping("/requestParam4") |
| 42 | public String main4(@RequestParam(required=false) String year) { |
| 43 | // http://localhost/ch2/requestParam4 ---->> year=null |
| 44 | // http://localhost/ch2/requestParam4?year ---->> year="" |
| 45 | System.out.printf("[%s]year=[%s]%n", new Date(), year); |
| 46 | return "yoil"; |
| 47 | } |
| 48 | |
| 49 | @RequestMapping("/requestParam5") |
| 50 | public String main5(@RequestParam(required=false, defaultValue="1") String year) { |
| 51 | // http://localhost/ch2/requestParam5 ---->> year=1 |
| 52 | // http://localhost/ch2/requestParam5?year ---->> year=1 |
| 53 | System.out.printf("[%s]year=[%s]%n", new Date(), year); |
| 54 | return "yoil"; |
| 55 | } |
| 56 | |
| 57 | // ======================================================================= |
| 58 | |
| 59 | @RequestMapping("/requestParam6") |
| 60 | public String main6(int year) { |
| 61 | // http://localhost/ch2/requestParam6 ---->> 500 java.lang.IllegalStateException: Optional int parameter 'year' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type. |
| 62 | // http://localhost/ch2/requestParam6?year ---->> 400 Bad Request, nested exception is java.lang.NumberFormatException: For input string: "" |
| 63 | System.out.printf("[%s]year=[%s]%n", new Date(), year); |
| 64 | return "yoil"; |
| 65 | } |
| 66 | |
| 67 | @RequestMapping("/requestParam7") |
| 68 | public String main7(@RequestParam int year) { |
nothing calls this directly
no outgoing calls
no test coverage detected