title: How to set response header in spring mvc
body: <p>I have a method in which i want to set response header cache-control and pragma :-</p>
  
  <pre><code>public String addUser(@Valid User user, BindingResult bindingResult)
  {
      if(bindingResult.hasErrors())
      {
          bindingResult.getFieldError();
          return"edit";
      }
      return "redirect:/welcome/profile/"+user.getName();
  }
  </code></pre>
  
  <p>In this method i want to set cache-control and pragma like we do in simple servlet code using HttpservletResponse calling setHeader method :-</p>
  
  <pre><code>response.setHeader("Cache-Control","no-cache,no-store,must-revalidate");
      response.setHeader("Pragma","no-cache");
      response.setDateHeader("Expires", 0);
  </code></pre>
  
  <p>I searched spring docs and could not find any direct way to do it, but I found this:-</p>
  
  <pre><code>@RequestMapping("/something")
  public ResponseEntity&lt;String&gt; handle(HttpEntity&lt;byte[]&gt; requestEntity) throws UnsupportedEncodingException 
  {
      String requestHeader = requestEntity.getHeaders().getFirst("MyRequestHeader"));
      byte[] requestBody = requestEntity.getBody();
      HttpHeaders responseHeaders = new HttpHeaders();
      responseHeaders.set("MyResponseHeader", "MyValue");
      return new ResponseEntity&lt;String&gt;("Hello World", responseHeaders, HttpStatus.CREATED);
  }
  </code></pre>
  
  <p>But I dont know how to use it</p>