Java annotations—annotations automatically generate the code necessary
for classes that use them to seamlessly connect within specific
frameworks.
REST Annotations
This annotation is used to map a web-request to a specific class or method in controller which will handle the corresponding request. We can apply this annotation for both at class level and method level. In class level we need to map the URL of the web-request (http request) and in method level we can map url or we can also map HTTP request methods. Example:-
contextConfigLocation
contextConfigLocation
/WEB-INF/spring-bean.xml,
/WEB-INF/spring-bean-ds.xml,
/WEB-INF/spring-bean-service.xml
JAX-RS provides some annotations to aid in mapping a resource class (a POJO) as a web resource. The annotations include:
REST Annotations
@XmlRootElement :
-
When a top level class or an enum type is annotated with
the @XmlRootElement annotation, then its value is represented as XML element in
an XML document.
Usage: - The
@XmlRootElement annotation can be used with the following program elements:
- a top level class
- an enum type
@Controller:-
This annotation indicates that a particular class serves as
a controller. The basic purpose of the
@Controller
annotation is to act as a stereotype for the annotated class, indicating its
role. The dispatcher will scan such annotated classes for mapped methods,
detecting @RequestMapping
annotations. This is easily achieved by
using the spring-context schema.
@Controller annotation in spring provides a feature of auto detection.
To enable auto detection of such annotated controllers, we have to add “component-scan”
in spring-context and needs to provide the base-package name or controller
class package name.
@RequestMapping
:-
This annotation is used to map a web-request to a specific class or method in controller which will handle the corresponding request. We can apply this annotation for both at class level and method level. In class level we need to map the URL of the web-request (http request) and in method level we can map url or we can also map HTTP request methods. Example:-
@RequestMapping(method = RequestMethod.GET,
value = "/getEmployees")
@RequestBody
:-
This annotation indicates that a method
parameter should be bound to the value of the HTTP request body.
@RequestMapping(method =
RequestMethod.POST, value = "/saveEmployee")
public ModelAndView
saveEmployeeInDB(@RequestBody String body) {
Source
source = new StreamSource(new StringReader(body));
EmpBean
e = (EmpBean) jaxb2Mashaller.unmarshal(source);
System.out.println("the val
from addEmployee-" + e.toString());
employeeDS.saveEmployee(e);
return new ModelAndView(XML_VIEW_NAME, "emp", e);
}
Here we are converting the request body
to the method argument by using an
In Spring 3.0 the HttpMessageConverter
.
HttpMessageConverter
is responsible for converting
from the HTTP request message to an object and converting from an object to the
HTTP response body. DispatcherServlet
supports annotation based processing using the DefaultAnnotationHandlerMapping
and AnnotationMethodHandlerAdapter
.AnnotationMethodHandlerAdapter
is extended to support the @RequestBody
and has the following HttpMessageConverters
registered by default:
·
ByteArrayHttpMessageConverter
converts byte arrays.
·
StringHttpMessageConverter
converts strings.
·
.
FormHttpMessageConverter
converts form data to/from a MultiValueMap
·
SourceHttpMessageConverter
converts to/from a javax.xml.transform.Source.
·
MarshallingHttpMessageConverter
converts to/from an object using the org.springframework.oxm
package.
Note: - Reference from springsource.org
@PathVariable: -
In Spring MVC we can use the
@PathVariable
annotation on a method argument to
bind it to the value of a URI template variable.
@RequestMapping(method =
RequestMethod.DELETE, value = "/removeEmployee/{id}")
public ModelAndView
removeEmployeeFromDB(@PathVariable String id) {
employeeDS.removeEmployee(Long.parseLong(id));
List
employees = employeeDS.getAllAfterDelete();
EmpListBean
list = new EmpListBean(employees);
return new ModelAndView(XML_VIEW_NAME, "empBeanList", list);
}
This will extract the part of the URL
represented by
Java 5 enumeration of HTTP request methods.
Intended for use with the RequestMapping.method
() attribute of the RequestMapping
annotation. Note that, by default, DispatcherServlet supports
GET, HEAD, POST, PUT and DELETE only.{id}
, and bind it to
the id
method parameter, e.g. taken
as a string.
contextConfigLocation: -(Note :- This is a parameter(init/context), not annotation)
This init-param is required by DispatcherServlet. Using this
parameter, we can change the name of Spring’s web context file and also we can change
its(Spring’s web context file) location.
/WEB-INF/restWebServiceWithCRUDOperations-context.xml
Note: - contextConfigLocation parameter will call
setContextConfigLocation
method on DispatcherServlet
and overrides default spring context
config file. It is also possible to add multiple locations separated by any
number of commas and space.@Consumes
This annotation works together with @POST and @PUT. It tells the framework to which method to delegate the incoming request.@Produces
This annotation works with @GET, @POST, and @PUT. It lets the framework know what type of representation to send back to the client.
JAX-RS provides some annotations to aid in mapping a resource class (a POJO) as a web resource. The annotations include:
- @Path specifies the relative path for a resource class or method.
- @GET, @PUT, @POST, @DELETE and @HEAD specify the HTTP request type of a resource.
- @Produces specifies the response Internet media types (used for content negotiation).
- @Consumes specifies the accepted request Internet media types.
- @PathParam binds the method parameter to a path segment.
- @QueryParam binds the method parameter to the value of an HTTP query parameter.
- @MatrixParam binds the method parameter to the value of an HTTP matrix parameter.
- @HeaderParam binds the method parameter to an HTTP header value.
- @CookieParam binds the method parameter to a cookie value.
- @FormParam binds the method parameter to a form value.
- @DefaultValue specifies a default value for the above bindings when the key is not found.
- @Context returns the entire context of the object.(for example
@Context HttpServletRequest request
)