RESTful service with Spring3 annotations

Creating RESTful services


@RequestMapping(value = {"restExampleString/{name}" }, method = RequestMethod.GET)
public @ResponseBody String restExampleString(@PathVariable String name){
String returnText;
returnText = "Welcome "+name+".";
return returnText;


A simple method annotated with @ResponceBody, acts as RESTful service. This is included in package org.springframework.web.  

We use URI to name the resource. we can get the information contained in the URI. {name} gives the information where as restExampleString gives the path to the service. 

sample URI for a RESTful service will be:
http://localhost:8080/appname/restExampleString/ris

@PathVarable:

Here ris contains the information passed to the RESTful service. @PathVariable annotation is used to get the information on to method variable. This annotation can also have the parameter(binding URI variable name to method variable).

@PathVariable("name") with this notation we can give any variable name to the method variable,but when we use only @PathVariable, we need to make sure that the URI variable ({name}) and Method variable(@PathVariable String name) are named same.

we can use multiple URI variables.
@RequestMapping(value = {"restExampleString/{name}/age/{age}" }, method = RequestMethod.GET)

@RequestBody:

HttpMessageConverter is responsible for converting 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. In Spring 3 the AnnotationMethodHandlerAdapterhas been extended to support the @RequestBody and has several HttpMessageConverters registered by default -- (source:docs.spring.io)


Simple rest service:

http://myrisha1.herokuapp.com/people/restExampleString/{name}

the name can take any value, and the method returns same name in string.

eg: http://myrisha1.herokuapp.com/people/restExampleString/rajendar bommidi

Welcome rajendar bommidi. in string format which can be used later.




tags:  RESTful services, spring3 with annotations, webservices, issues in rest services, 

0 comments:

Contact

Talk to us

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolores iusto fugit esse soluta quae debitis quibusdam harum voluptatem, maxime, aliquam sequi. Tempora ipsum magni unde velit corporis fuga, necessitatibus blanditiis.

Address:

9983 City name, Street name, 232 Apartment C

Work Time:

Monday - Friday from 9am to 5pm

Phone:

595 12 34 567

fb