Get all updates of Java4s
Follow Us: Email Newsletter

JAX-RS Example of Multiple Resource Formats

This article will describe how a RESTful web service produces multiple output formats.  In the previous articles we came across how a RESTful service produces either XML or JSON alone as an output, but in this article i will show you what are the changes or steps need to be followed to let the rest service to produce multiple output formats from a single method, its the real time way of creating the services.  Before you start reading this article, just have a look at the following articles for better understanding.

Exactly, exactly this current example is the combination of above two articles :-) I am just copy & pasting those articles and doing some changes here that’s it.

Required Files

  • pom.xml
  • web.xml
  • Customer.java
  • JsonFromRestful.java
  • RESTfulClient.java

Directory Structure

pom.xml

web.xml

Customer.java

JsonFromRestful.java

RESTfulClient.java

Execution & Output

  • Eclipse > right click on RESTfulClient.java > Run As > Run on Server
  • As per above program it will give you the JSON output
  • If you want to see the output in XML format. Open RESTfulClient.java > in line number 14 replace that line with

    ClientResponse response = resource.accept(“application/xml“).get(ClientResponse.class);

    and the XML output will be

 

You Might Also Like

  ♦ About Author ♦

Java4s_Author
sivateja@java4s.com
Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

19 Responses to “JAX-RS Example of Multiple Resource Formats”
  1. Madiraju Krishna Chaitanya says:

    Hi Sivateja Kandula Ji,Good Morning.Thanks a LOT for providing and sharing knowledge on WebServices with all of us.I was searching for an authentic content with real time examples,that is when I found your site.One small favor from your side.Can you please share a real-time project(or a dummy one) that you have created w.r.t SOAP UI and RESTful webservices…?.I am learning webservices concepts and I would like to have a projects(for SOAP and RESTful) for my understanding.Kindly share the project code if you have with me.Thanks in Advance.
    You can share the code(project)with my email id: chaitanya.mca@gmail.com.Thanks Again. ~Chaitanya.

  2. I don’t know how much effort that Java4S team kept in making this Restful web services tutorial but for me it tooks just 4-5 hours to complete all the examples…i don’t know much about web services before visiting this site.

    Thanks a lot to Java4S team. Wish your team to keep up the good work.

  3. Thirupathi says:

    Hi Java4s Team,
    You are providing good tutorial on Java.. Everyday I’m following your site. I have learnt lot of frameworks from your site.Thank you so much. Please could you provide JSF tutorial also.Anyways thanks..

  4. sandeep says:

    Hi dear,
    This is very helpfull site for beginner to learn the webservice.

  5. Varshit says:

    hi sivateja,

    it is amazing, before visiting this site, i was having the small doubts, but now it is clear.. very nice tutor…
    sivateja, can u provide any sample or dumy real time project or steps to develop the real time project. it will be very useful to me.
    If u kindly provide the project on RESTful. Kindly share the project code if you have. Thanks in Advance. You can share the code(project)with my email id – asrikanthjava9885@gmail.com
    waiting for your positive reply

    Thks…

  6. Binoy KB says:

    Just Awesome Tutorial on Web Service.

  7. Venkata says:

    Hi Sir,your Tutorials are the BEST among the Web for Java.looking for Further java Stuff from you.Thanks Venkata Sriram

  8. Mahi says:

    hi Dear,
    Nice Tutorial . I was seeking good material about REST Full web services from the 2 year but did not get any good material .the way you write is it is amazing its very help full to the beginner . they can learn quick.

    One more request to you can you provide a real example of client Rest full .
    Suppose i have published the Web services and consumer wants to access this web service . how they can access I mean have to do tie up between consumer and publisher. other wise any body can acccess thier web service if I know the URL of Web service.

  9. kesavamoorthy says:

    Hi Java4s Team,

    Its realy good tutorial for all, i learn lot form this site and please work on more core java.

    Thanks ,
    kesavan

  10. kesavamoorthy says:

    Hi ,

    Can you give some example of Restful service Authentication.

  11. indu says:

    Nice Tutorial . I was looking real time project on RESTful web services.
    can u provide steps how to configure RESTful with spring.

    kindly provide the project on RESTful. Kindly share the project code if you have. Thanks in Advance. You can share the code(project)with my email id.

  12. Ganesh says:

    Hi Sivateja,

    I saw your tutorial ,i got a few knowledge on web service , thank you for sharing examples and web service picture.

  13. Reddy says:

    hi Indu

    I have done some RESTful (jersey,apache cxf) examples using spring.if u want drop a mail to akkireddy61@gmail.com.

  14. Rahul Nirgude says:

    Hi.. thanks for this website. This is really helpful

  15. venkat says:

    Hi

    I was followed u r site In this site I learned good things but now I am learning web services but I want real time project . I hope u definitely will share the project code ASAP..

    this is my mail id venkt.java008@gmail.com

  16. Raajkiran says:

    Hi Java4s Team,

    I was followed you web services tutorials regularly, here i want to know how to apply these restful concepts in project.can you help me ,if you have project(dummy) you can send me this email:rajkiran.rajkumar@gmail.com.Thanks in advance.

  17. Suvakant says:

    Hi java4s Team,
    I really impressed with the tutorial, dont know how much effort u guys put for this, for me it took hardly take 4 hr to learn webservice, i was not aware of any thing priorto that , i am prety much confident on webservice now. Thnks a lot team :)

  18. Sasmita says:

    Thanks java4s team. Very very good tutotial.

  19. Nivas says:

    Thanks a lot.. Really nice explanation on each terms. Keep the good work..

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2016 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.