Files
spring-boot-rest/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/ResourceEndpoint.java
2017-02-04 23:50:47 +00:00

36 lines
1.3 KiB
Java

package com.baeldung.cachecontrol;
import com.baeldung.cachecontrol.model.TimestampDto;
import com.baeldung.cachecontrol.model.UserDto;
import org.springframework.http.CacheControl;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.concurrent.TimeUnit;
@RestController
public class ResourceEndpoint {
@RequestMapping(value = "/users/{name}", method = RequestMethod.GET)
public ResponseEntity<UserDto> getUser(@PathVariable(value = "name") String name) {
return ResponseEntity.ok()
.cacheControl(CacheControl.maxAge(60, TimeUnit.SECONDS))
.body(new UserDto(name));
}
@RequestMapping(value = "/timestamp", method = RequestMethod.GET)
public ResponseEntity<TimestampDto> getServerTimestamp() {
return ResponseEntity.ok()
.cacheControl(CacheControl.noStore())
.cacheControl(CacheControl.noCache())
.body(new TimestampDto(LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli()));
}
}