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