simplify controller method
This commit is contained in:
parent
7743035b4d
commit
098cd16514
|
@ -24,30 +24,39 @@ public class CustomResponseController {
|
|||
|
||||
@GetMapping("/age")
|
||||
public ResponseEntity<String> age(@RequestParam("yearOfBirth") int yearOfBirth) {
|
||||
int currentYear = Year.now().getValue();
|
||||
|
||||
if (currentYear < yearOfBirth) {
|
||||
if (isInFuture(yearOfBirth)) {
|
||||
return new ResponseEntity<>("Year of birth cannot be in the future", HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
int age = currentYear - yearOfBirth;
|
||||
|
||||
return new ResponseEntity<>("Your age is " + age, HttpStatus.OK);
|
||||
return new ResponseEntity<>("Your age is " + calculateAge(yearOfBirth), HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
private int calculateAge(int yearOfBirth) {
|
||||
return currentYear() - yearOfBirth;
|
||||
}
|
||||
|
||||
private boolean isInFuture(int year) {
|
||||
return currentYear() < year;
|
||||
}
|
||||
|
||||
private int currentYear() {
|
||||
return Year.now().getValue();
|
||||
}
|
||||
|
||||
@GetMapping("/customHeader")
|
||||
public ResponseEntity<String> customHeader() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("Custom-Header", "foo");
|
||||
|
||||
|
||||
return new ResponseEntity<>("Custom header set", headers, HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/manual")
|
||||
public void manual(HttpServletResponse response) throws IOException {
|
||||
response.setHeader("Custom-Header", "foo");
|
||||
response.setStatus(200);
|
||||
response.getWriter().println("Hello World!");
|
||||
response.getWriter()
|
||||
.println("Hello World!");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue