Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does context annotation do in Spring?

Tags:

spring

jersey

In Rest API design, I am wondering what the exact purpose of the context annotation is?

private HttpServletRequest request;

 @Context
 public void setRequest(final HttpServletRequest req) {
        request = req;
 }
like image 842
Adam Lee Avatar asked Jun 09 '12 16:06

Adam Lee


2 Answers

The purpose is to indicate that the request property should be set from the context.

@Context is used to inject various HTTP-ish contextual data, from here:

In general @Context can be used to obtain contextual Java types related to the request or response.

API docs (Not horribly useful IMO. Or, perhaps more accurately, horribly-useful.)

like image 194
Dave Newton Avatar answered Nov 03 '22 09:11

Dave Newton


This annotation is used to inject information into a class field, bean property or method parameter.

JAX-RS @Context to get the ServletContext, and WebApplicationContextUtils to get the Spring application context, with this Spring application context, you are able to access and get beans from Spring container

like image 44
Rajkumar Singh Avatar answered Nov 03 '22 09:11

Rajkumar Singh