Viren Pushpanayagam
Viren Pushpanayagam

Reputation: 2497

Spring MVC - Response

How can I access the response object from a bean? To get the request object I use the following.

    ServletRequestAttributes attr = (ServletRequestAttributes) 
        RequestContextHolder.currentRequestAttributes();

Is there something similar to the above for response object?

Upvotes: 3

Views: 4875

Answers (1)

Rob Beardow
Rob Beardow

Reputation: 871

If you are in a web application context (which it looks like you are) you can auto wire in the HttpServletRequest or HttpServletResponse.

The request/response from the current request scope will be injected.

@Component
public class SomeComponentInAWebApplicationContext {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private HttpServletResponse response;

    ...
}

Upvotes: 5

Related Questions