Refactor ResourceEndpoint

This commit is contained in:
pivovarit 2017-02-05 17:06:12 +01:00
parent 7664601b7d
commit 5b79f605a4
2 changed files with 22 additions and 12 deletions

View File

@ -14,6 +14,11 @@
<version>1.4.3.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -1,6 +1,5 @@
package com.baeldung.cachecontrol;
import com.baeldung.cachecontrol.model.TimestampDto;
import com.baeldung.cachecontrol.model.UserDto;
import org.springframework.http.CacheControl;
@ -21,23 +20,29 @@ public class ResourceEndpoint {
return ResponseEntity.ok(new UserDto(name));
}
@GetMapping(value = "/users/{name}")
@GetMapping("/users/{name}")
public ResponseEntity<UserDto> getUser(@PathVariable String name) {
return ResponseEntity.ok()
return ResponseEntity
.ok()
.cacheControl(CacheControl.maxAge(60, TimeUnit.SECONDS))
.body(new UserDto(name));
}
@GetMapping(value = "/timestamp")
@GetMapping("/timestamp")
public ResponseEntity<TimestampDto> getServerTimestamp() {
return ResponseEntity.ok()
return ResponseEntity
.ok()
.cacheControl(CacheControl.noStore())
.body(new TimestampDto(LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli()));
.body(new TimestampDto(LocalDateTime
.now()
.toInstant(ZoneOffset.UTC)
.toEpochMilli()));
}
@GetMapping(value = "/private/users/{name}")
@GetMapping("/private/users/{name}")
public ResponseEntity<UserDto> getUserNotCached(@PathVariable String name) {
return ResponseEntity.ok()
return ResponseEntity
.ok()
.body(new UserDto(name));
}
}