Saturday, March 16, 2013

Create your web services with Spring

In this post I will show "Spring way" to build RESTful web services. It also explained how to produce multiple representations (JSON, XML and etc.), which is an important feature for RESTful web services.
This article explains another way to produce multiple representations using HttpMessageConverter, and examples in the article show how to use RestTemplate with HttpMessageConverter to communicate with services. HTTP requests and responses are text based, meaning a browser and server communicate by exchanging raw texts. With Spring, however, methods in the controller class return pure 'String' type and domain models (or other Java built-in objects). How can Spring serialize/de-serialize the objects to raw texts? This is handled by HttpMessageConverter. Spring has bundled implementations that can meet your common needs. Table 1 shows some examples.



With...You can...
StringHttpMessageConverterRead/write a string from request and response. By default, it supports the media type text/* and writes with a Content-Type of text/plain.

FormHttpMessageConverterRead/write form data from request and response. By default, it reads the media type application/x-www-form-urlencoded and writes data into MultiValueMap<String,String>.

MarshallingHttpMessageConverterRead/write XML data using Spring's marshaller/un-marshaller. It converts data of media type application/xml.

MappingJacksonHttpMessageConverterRead/write JSON data using Jackson's ObjectMapper. It converts data of media type application/json.

AtomFeedHttpMessageConverterRead/write ATOM feed using ROME's Feed API. It converts data of media type application/atom+xml.

RssChannelHttpMessageConverterRead/write RSS feed using ROME's feed API. It converts data of media type application/rss+xml.


It's time 4 demo :)

@Controller
@RequestMapping(value = "/wss2", headers = "Accept=application/json, application/xml")
public class WebServiceController2 {
 
 private static Logger log = Logger.getLogger(WebServiceController2.class);
 
 @Autowired private ServiceRegistry serviceRegistry;
 
 
 @RequestMapping(method = RequestMethod.GET, value = "/users/{email:.*}")
 @ResponseBody
    public UserProfile showUserData(@PathVariable String email) {
  return serviceRegistry.userService.getUserProfile(email);
    }
 
 
 
 
 
 @RequestMapping(method = RequestMethod.PUT, value = "/users/{email:.*}")
 @ResponseBody
 public UserProfile updateUserData(@PathVariable String email, @RequestBody UserProfile userProfile) {
  
  serviceRegistry.userService.saveUser(log, userProfile);
    
  return serviceRegistry.userService.getUserProfile(email);
 }

}
Now u can test your service from RESTClient. If u set header "Accept:application/json" u will get json response, but if u set "Accept:application/xml" then response will be in XML format.

Note: If u get "Status Code: 406 Not Acceptable" (The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.) then please check to things:
1. Is there all jars are in lib folder
2. Is your object has "@XmlRootElement" annotation:

...
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class UserProfile implements Serializable {
...
}
So I think Spring is the best Open Source Java framework, isn't it? :)

2 comments:

  1. Wow really nice to gain knowledge about your blog.Nice blog.we are also providing web services.Kamalaminfotech is the best Web design,development and web hosting Solutions is a professionally handled with Domain Registration. We provide services of free Domain Registration and Cheap Web Hosting Company In Madurai and India.Domain registration and hosting is to be first method towards creating a website.
    Visit Us: Domain Registration Madurai | Web hosting Madurai

    ReplyDelete
  2. This blog is very impressive and this can help in introducing many representations.

    PHILWEBSERVICES, INC.

    ReplyDelete