java 设置响应头_java – 如何在spring mvc中设置响应头

我有一个方法,我想设置响应头缓存控制和pragma: –

public String addUser(@Valid User user, BindingResult bindingResult)

{

if(bindingResult.hasErrors())

{

bindingResult.getFieldError();

return"edit";

}

return "redirect:/welcome/profile/"+user.getName();

}

在这种方法中,我想要像使用HttpservletResponse调用setHeader方法在简单的servlet代码中设置缓存控制和pragma:

response.setHeader("Cache-Control","no-cache,no-store,must-revalidate");

response.setHeader("Pragma","no-cache");

response.setDateHeader("Expires", 0);

我搜索了spring文档,找不到任何直接的方法,但是我发现:

@RequestMapping("/something")

public ResponseEntity handle(HttpEntity requestEntity) throws UnsupportedEncodingException

{

String requestHeader = requestEntity.getHeaders().getFirst("MyRequestHeader"));

byte[] requestBody = requestEntity.getBody();

HttpHeaders responseHeaders = new HttpHeaders();

responseHeaders.set("MyResponseHeader", "MyValue");

return new ResponseEntity("Hello World", responseHeaders, HttpStatus.CREATED);

}

但我不知道如何使用它

你可能感兴趣的:(java,设置响应头)