Close

Spring MVC - Setting request and response Headers Examples

[Last Updated: Nov 8, 2025]

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 jakarta.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.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.ServletWebRequest;
import java.io.IOException;

@Controller
public class TheController {
@Autowired
private ResourceService resourceService;

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

//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(resourceService.lastModified())) {
//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 resourceService.read();
}


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

//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(resourceService.lastModified())
.body(resourceService.read());
}

@ResponseBody
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String updateResource(@RequestBody String content) throws IOException {
resourceService.update(content);
return "Resource updated";
}
}
Original Post




Working with ETag and If-None-Match headers.

package com.logicbig.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;

@Controller
public class TheController {
@Autowired
private ResourceStore resourceStore;

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

if (swr.checkNotModified(getETag())) {
//it will return 304 with empty body
return null;
}
return resourceStore.getCurrentResource().getData();
}

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

return ResponseEntity
.ok()
.eTag(getETag())
.body(resourceStore.getCurrentResource().getData());
}

@ResponseBody
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String updateResource(@RequestBody String content) {
resourceStore.update(content);
return "Resource updated";
}

public String getETag() {
return "version" +
resourceStore.getCurrentResource().getVersion();
}
}
Original Post




See Also

Join