@GetMapping("/age") ResponseEntity<String> age(@RequestParam("yearOfBirth")int yearOfBirth) { if (isInFuture(yearOfBirth)) { return ResponseEntity.badRequest() .body("Year of birth cannot be in the future"); }
return ResponseEntity.status(HttpStatus.OK) .body("Your age is " + calculateAge(yearOfBirth)); }
1.3. 简单使用
1 2 3 4 5 6 7
@GetMapping("/age") ResponseEntity<String> age(@RequestParam("yearOfBirth")int yearOfBirth) { if (isInFuture(yearOfBirth)) { returnnewResponseEntity<>("Year of birth cannot be in the future", HttpStatus.BAD_REQUEST); } returnnewResponseEntity<>("Your age is " + calculateAge(yearOfBirth), HttpStatus.OK); }
评论