1

I have a little java spring project, it is an API that returns xml response. Want to add Cache-Control header to response but im not able to find out how. Can I add headers to response or I have to move this to MVC?

@RequestMapping(method=RequestMethod.GET,value="/search/discountsall", 
        produces="application/xml")
public ModelAndView searchAll() {
    SearchDiscounts discounts = new SearchDiscounts();

    discounts = serviceFacade.getAll();

    ModelAndView mav = new ModelAndView("xmlView");
    mav.addObject("discounts", discounts);

    return mav;
}

Here is my rest-servlet.xml header:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">
Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • 1
    Add `HttpServletResponse` as argument to your `searchAll` method and you can do whatever you want. However if you want to be able to reuse the logic you might want to use a `HandlerInterceptor` or plain servlet `Filter` instead. – M. Deinum Nov 07 '13 at 15:54
  • Thanks @M.Deinum what library should I add? I cant find any reference to HttpServletResponse at import org.springframework.web.servlet. – user2965475 Nov 07 '13 at 16:19
  • That is a standard servlet class and has nothing to do with Spring. – M. Deinum Nov 08 '13 at 10:17
  • possible duplicate of [How do you set cache headers in Spring MVC?](http://stackoverflow.com/questions/1362930/how-do-you-set-cache-headers-in-spring-mvc) – Joe Feb 14 '15 at 13:31

0 Answers0