Java 如何在spring mvc中设置响应头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18534478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set response header in spring mvc
提问by M Kumar
I have a method in which i want to set response header cache-control and pragma :-
我有一种方法,我想在其中设置响应标头缓存控制和编译指示:-
public String addUser(@Valid User user, BindingResult bindingResult)
{
if(bindingResult.hasErrors())
{
bindingResult.getFieldError();
return"edit";
}
return "redirect:/welcome/profile/"+user.getName();
}
In this method i want to set cache-control and pragma like we do in simple servlet code using HttpservletResponse calling setHeader method :-
在这种方法中,我想设置缓存控制和编译指示,就像我们在简单的 servlet 代码中使用 HttpservletResponse 调用 setHeader 方法一样:-
response.setHeader("Cache-Control","no-cache,no-store,must-revalidate");
response.setHeader("Pragma","no-cache");
response.setDateHeader("Expires", 0);
I searched spring docs and could not find any direct way to do it, but I found this:-
我搜索了 spring 文档并找不到任何直接的方法来做到这一点,但我发现了这个:-
@RequestMapping("/something")
public ResponseEntity<String> handle(HttpEntity<byte[]> requestEntity) throws UnsupportedEncodingException
{
String requestHeader = requestEntity.getHeaders().getFirst("MyRequestHeader"));
byte[] requestBody = requestEntity.getBody();
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("MyResponseHeader", "MyValue");
return new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED);
}
But I dont know how to use it
但是不知道怎么用
采纳答案by Andrei N
If you want to set headers for every response for a controller you can use @ModelAttribute
annotation.
如果您想为控制器的每个响应设置标头,您可以使用@ModelAttribute
注释。
@ModelAttribute
public void setVaryResponseHeader(HttpServletResponse response) {
response.setHeader("Vary", "Accept");
}
回答by Prabhakaran Ramaswamy
public String addUser(@Valid User user, BindingResult bindingResult,HttpServletRequest request,HttpServletResponse response)
{
if(bindingResult.hasErrors())
{
bindingResult.getFieldError();
return"edit";
}
response.setHeader("Cache-Control","no-cache,no-store,must-revalidate");
response.setHeader("Pragma","no-cache");
response.setDateHeader("Expires", 0);
return "redirect:/welcome/profile/"+user.getName();
}
回答by Антон Терёшин
Since Spring v. 4.1:
从 Spring v. 4.1 开始:
@RequestMapping("/something")
public ResponseEntity<MyClass> handle()
{
return ResponseEntity.status(HttpStatus.CREATED)
.header("header", "value")
.body(new MyClass());
}
回答by Ruchira
You can manually set headers as follows.
您可以手动设置标题如下。
imports:
进口:
import org.springframework.http.HttpHeaders;
code:
代码:
public ResponseEntity<Map<String, Object>> doSomething() {
...
HttpHeaders respHeaders = new HttpHeaders();
respHeaders.add("Pragma", "no-cache");
respHeaders.add("Cache-Control","no-cache,no-store,must-revalidate");
return new ResponseEntity<Map<String, Object>>(respHeaders, HttpStatus.OK);
}