Close

Spring MVC - Setting request and response Headers Examples

Spring MVC 

Setting 'Cache-Control' with 'max-age' directive.

package com.logicbig.example;

import org.springframework.http.CacheControl;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

@Controller
public class TheController {

@RequestMapping(value = "/test1")
public String handle1 (HttpServletResponse response) {
String headerValue = CacheControl.maxAge(10, TimeUnit.SECONDS)
.getHeaderValue();

response.addHeader("Cache-Control", headerValue);
return "myView";
}

@ResponseBody
@RequestMapping(value = "/test2")
public ResponseEntity<String> handle2 () {

CacheControl cacheControl = CacheControl.maxAge(10, TimeUnit.SECONDS);

String testBody = "<p>Response time: " + LocalDateTime.now() +
"</p><a href=''>test2</a>";
return ResponseEntity.ok()
.cacheControl(cacheControl)
.body(testBody);
}
}
Original Post




Setting 'Last-Modified' header to response. Also checking 'If-Modified-Since' header in the request.

package com.logicbig.example;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

@Controller
public class TheController {

public static long getResourceLastModified () {
ZonedDateTime zdt = ZonedDateTime.of(LocalDateTime.of(2017, 1, 9,
10, 30, 20),
ZoneId.of("GMT"));
return zdt.toInstant().toEpochMilli();
}

@RequestMapping(value = "/test1")
public String handle1 (ServletWebRequest swr) {

//doesn't matter it returns false/true it will set the required headers automatically.
//It doesn't include 'Cache-Control:no-cache' so have to do browser F5
if (swr.checkNotModified(getResourceLastModified())) {
//it will return 304 with empty body
return null;
}

//uncomment the following if last-modified checking is needed at every action
/* swr.getResponse().setHeader(HttpHeaders.CACHE_CONTROL,
CacheControl.noCache()
.getHeaderValue());*/

return "myView";
}

@ResponseBody
@RequestMapping(value = "/test2")
public String handle2 (WebRequest swr) {

if (swr.checkNotModified(getResourceLastModified())) {
return null;
}

String testBody = "<p>Response time: " + LocalDateTime.now() +
"</p><a href='test2'>test2</a>";

return testBody;
}

@ResponseBody
@RequestMapping(value = "/test3")
public ResponseEntity<String> handle3 (WebRequest swr) {

String testBody = "<p>Response time: " + LocalDateTime.now() +
"</p><a href='test3'>test3</a>";

//returning ResponseEntity with lastModified, HttpEntityMethodProcessor will
//take care of populating/processing the required headers.
//As the body can be replaced with empty one and 304 status can be send back,
// this approach should be avoided if preparing the response body is very expensive.
return ResponseEntity.ok()
.lastModified(getResourceLastModified())
.body(testBody);
}
}
Original Post




Working with ETag and If-None-Match headers.

package com.logicbig.example;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;

import java.time.LocalDateTime;

@Controller
public class TheController {

@RequestMapping(value = "/test1")
public String handle1 (ServletWebRequest swr) {

if (swr.checkNotModified(getETag())) {
//it will return 304 with empty body
return null;
}
return "myView";
}

@ResponseBody
@RequestMapping(value = "/test2")
public String handle2 (WebRequest swr) {

if (swr.checkNotModified(getETag())) {
return null;
}

String testBody = "<p>Response time: " + LocalDateTime.now() +
"</p><a href='test2'>test2</a>";

return testBody;
}

@ResponseBody
@RequestMapping(value = "/test3")
public ResponseEntity<String> handle3 (WebRequest swr) {

String version = getETag();

String testBody = "<p>Response time: " + LocalDateTime.now() +
"</p><a href='test3'>test3</a>";
return ResponseEntity
.ok()
.eTag(version)
.body(testBody);
}

public String getETag () {
return "version1";
}
}
Original Post




See Also