|
| 1 | +package com.baeldung.cachecontrol; |
| 2 | + |
| 3 | + |
| 4 | +import com.baeldung.cachecontrol.model.TimestampDto; |
| 5 | +import com.baeldung.cachecontrol.model.UserDto; |
| 6 | +import org.springframework.http.CacheControl; |
| 7 | +import org.springframework.http.ResponseEntity; |
| 8 | +import org.springframework.web.bind.annotation.PathVariable; |
| 9 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 10 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 11 | +import org.springframework.web.bind.annotation.RestController; |
| 12 | + |
| 13 | +import java.time.LocalDateTime; |
| 14 | +import java.time.ZoneOffset; |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | + |
| 17 | +@RestController |
| 18 | +public class ResourceEndpoint { |
| 19 | + |
| 20 | + @RequestMapping(value = "/users/{name}", method = RequestMethod.GET) |
| 21 | + public ResponseEntity<UserDto> getUser(@PathVariable(value = "name") String name) { |
| 22 | + return ResponseEntity.ok() |
| 23 | + .cacheControl(CacheControl.maxAge(60, TimeUnit.SECONDS)) |
| 24 | + .body(new UserDto(name)); |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + @RequestMapping(value = "/timestamp", method = RequestMethod.GET) |
| 29 | + public ResponseEntity<TimestampDto> getServerTimestamp() { |
| 30 | + return ResponseEntity.ok() |
| 31 | + .cacheControl(CacheControl.noStore()) |
| 32 | + .cacheControl(CacheControl.noCache()) |
| 33 | + .body(new TimestampDto(LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli())); |
| 34 | + } |
| 35 | +} |
0 commit comments