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> <version>1.4.3.RELEASE</version>
</parent> </parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

View File

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