@GetMapping("/age")ResponseEntity<String>age(@RequestParam("yearOfBirth")int yearOfBirth){if(isInFuture(yearOfBirth)){returnResponseEntity.badRequest().body("Year of birth cannot be in the future");}returnResponseEntity.status(HttpStatus.OK).body("Your age is "+calculateAge(yearOfBirth));}
1.3. 简单使用
@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);}