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... |
---|---|
StringHttpMessageConverter | Read/write a string from request and response. By default, it
supports the media type text/* and writes with a Content-Type of
text/plain. |
FormHttpMessageConverter | Read/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>. |
MarshallingHttpMessageConverter | Read/write XML data using Spring's marshaller/un-marshaller. It
converts data of media type application/xml. |
MappingJacksonHttpMessageConverter | Read/write JSON data using Jackson's
ObjectMapper . It converts data of media type
application/json. |
AtomFeedHttpMessageConverter | Read/write ATOM feed using ROME's Feed API. It converts data of
media type application/atom+xml. |
RssChannelHttpMessageConverter | Read/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? :)
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.
ReplyDeleteVisit Us: Domain Registration Madurai | Web hosting Madurai
This blog is very impressive and this can help in introducing many representations.
ReplyDeletePHILWEBSERVICES, INC.
Shared web hosting is the best form of web hosting if you are looking for a great price and don't have more than a couple thousand daily visitors to your site. hosting domain
ReplyDelete