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 AnnotationMethodHandlerAdapter
has 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:
Post a Comment