RESTful Web Services – First Application ”; Previous Next Let us start writing the actual RESTful web services with Jersey Framework. Before you start writing your first example using the Jersey Framework, you have to make sure that you have setup your Jersey environment properly as explained in the RESTful Web Services – Environment Setup chapter. Here, I am also assuming that you have a little working knowledge of Eclipse IDE. So, let us proceed to write a simple Jersey Application which will expose a web service method to display the list of users. Creating a Java Project The first step is to create a Dynamic Web Project using Eclipse IDE. Follow the option File → New → Project and finally select the Dynamic Web Project wizard from the wizard list. Now name your project as UserManagement using the wizard window as shown in the following screenshot − Once your project is created successfully, you will have the following content in your Project Explorer − Adding the Required Libraries As a second step let us add Jersey Framework and its dependencies (libraries) in our project. Copy all jars from following directories of download jersey zip folder in WEB-INF/lib directory of the project. jaxrs-ri-2.17jaxrs-riapi jaxrs-ri-2.17jaxrs-riext jaxrs-ri-2.17jaxrs-rilib Now, right click on your project name UserManagement and then follow the option available in context menu − Build Path → Configure Build Path to display the Java Build Path window. Now use Add JARs button available under Libraries tab to add the JARs present in WEBINF/lib directory. Creating the Source Files Now let us create the actual source files under the UserManagement project. First we need to create a package called com.tutorialspoint. To do this, right click on src in package explorer section and follow the option − New → Package. Next we will create UserService.java, User.java,UserDao.java files under the com.tutorialspoint package. User.java package com.tutorialspoint; import java.io.Serializable; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = “user”) public class User implements Serializable { private static final long serialVersionUID = 1L; private int id; private String name; private String profession; public User(){} public User(int id, String name, String profession){ this.id = id; this.name = name; this.profession = profession; } public int getId() { return id; } @XmlElement public void setId(int id) { this.id = id; } public String getName() { return name; } @XmlElement public void setName(String name) { this.name = name; } public String getProfession() { return profession; } @XmlElement public void setProfession(String profession) { this.profession = profession; } } UserDao.java package com.tutorialspoint; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.List; public class UserDao { public List<User> getAllUsers(){ List<User> userList = null; try { File file = new File(“Users.dat”); if (!file.exists()) { User user = new User(1, “Mahesh”, “Teacher”); userList = new ArrayList<User>(); userList.add(user); saveUserList(userList); } else{ FileInputStream fis = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(fis); userList = (List<User>) ois.readObject(); ois.close(); } } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return userList; } private void saveUserList(List<User> userList){ try { File file = new File(“Users.dat”); FileOutputStream fos; fos = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(userList); oos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } UserService.java package com.tutorialspoint; import java.util.List; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path(“/UserService”) public class UserService { UserDao userDao = new UserDao(); @GET @Path(“/users”) @Produces(MediaType.APPLICATION_XML) public List<User> getUsers(){ return userDao.getAllUsers(); } } There are two important points to be noted about the main program, UserService.java The first step is to specify a path for the web service using @Path annotation to the UserService. The second step is to specify a path for the particular web service method using @Path annotation to method of UserService. Creating the Web.xml configuration File You need to create a Web xml Configuration file which is an XML file and is used to specify Jersey framework servlet for our application. web.xml <?xml version = “1.0” encoding = “UTF-8”?> <web-app xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xmlns = “http://java.sun.com/xml/ns/javaee” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd” id = “WebApp_ID” version = “3.0”> <display-name>User Management</display-name> <servlet> <servlet-name>Jersey RESTful Application</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.tutorialspoint</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Jersey RESTful Application</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app> Deploying the Program Once you are done with creating source and web configuration files, you are ready for this step which is compiling and running your program. To do this, using Eclipse, export your application as a war file and deploy the same in tomcat. To create a WAR file using eclipse, follow the option File → export → Web → War File and finally select project UserManagement and destination folder. To deploy a war file in Tomcat, place the UserManagement.war in the Tomcat Installation Directory → webapps directory and start the Tomcat. Running the Program We are using Postman, a Chrome extension, to test our webservices. Make a request to UserManagement to get list of all the users. Put http://localhost:8080/UserManagement/rest/UserService/users in POSTMAN with GET request and see the following result. Congratulations, you have created your first RESTful Application successfully. Print Page Previous Next Advertisements ”;
Category: restful
RESTful – Caching
RESTful Web Services – Caching ”; Previous Next Caching refers to storing the server response in the client itself, so that a client need not make a server request for the same resource again and again. A server response should have information about how caching is to be done, so that a client caches the response for a time-period or never caches the server response. Following are the headers which a server response can have in order to configure a client”s caching − Sr.No. Header & Description 1 Date Date and Time of the resource when it was created. 2 Last Modified Date and Time of the resource when it was last modified. 3 Cache-Control Primary header to control caching. 4 Expires Expiration date and time of caching. 5 Age Duration in seconds from when resource was fetched from the server. Cache-Control Header Following are the details of a Cache-Control header − Sr.No. Directive & Description 1 Public Indicates that resource is cacheable by any component. 2 Private Indicates that resource is cacheable only by the client and the server, no intermediary can cache the resource. 3 no-cache/no-store Indicates that a resource is not cacheable. 4 max-age Indicates the caching is valid up to max-age in seconds. After this, client has to make another request. 5 must-revalidate Indication to server to revalidate resource if max-age has passed. Best Practices Always keep static contents like images, CSS, JavaScript cacheable, with expiration date of 2 to 3 days. Never keep expiry date too high. Dynamic content should be cached for a few hours only. Print Page Previous Next Advertisements ”;
RESTful – Introduction
RESTful Web Services – Introduction ”; Previous Next What is REST architecture? REST stands for REpresentational State Transfer. REST is web standards based architecture and uses HTTP Protocol. It revolves around resource where every component is a resource and a resource is accessed by a common interface using HTTP standard methods. REST was first introduced by Roy Fielding in 2000. In REST architecture, a REST Server simply provides access to resources and REST client accesses and modifies the resources. Here each resource is identified by URIs/ global IDs. REST uses various representation to represent a resource like text, JSON, XML. JSON is the most popular one. HTTP methods Following four HTTP methods are commonly used in REST based architecture. GET − Provides a read only access to a resource. POST − Used to create a new resource. DELETE − Used to remove a resource. PUT − Used to update a existing resource or create a new resource. Introduction to RESTFul web services A web service is a collection of open protocols and standards used for exchanging data between applications or systems. Software applications written in various programming languages and running on various platforms can use web services to exchange data over computer networks like the Internet in a manner similar to inter-process communication on a single computer. This interoperability (e.g., between Java and Python, or Windows and Linux applications) is due to the use of open standards. Web services based on REST Architecture are known as RESTful web services. These webservices uses HTTP methods to implement the concept of REST architecture. A RESTful web service usually defines a URI, Uniform Resource Identifier a service, provides resource representation such as JSON and set of HTTP Methods. Creating RESTFul Webservice In next chapters, we”ll create a webservice say user management with following functionalities − Sr.No. URI HTTP Method POST body Result 1 /UserService/users GET empty Show list of all the users. 2 /UserService/addUser POST JSON String Add details of new user. 3 /UserService/getUser/:id GET empty Show details of a user. Print Page Previous Next Advertisements ”;
RESTful – Statelessness
RESTful Web Services – Statelessness ”; Previous Next As per the REST architecture, a RESTful Web Service should not keep a client state on the server. This restriction is called Statelessness. It is the responsibility of the client to pass its context to the server and then the server can store this context to process the client”s further request. For example, session maintained by server is identified by session identifier passed by the client. RESTful Web Services should adhere to this restriction. We have seen this in the RESTful Web Services – Methods chapter, that the web service methods are not storing any information from the client they are invoked from. Consider the following URL − https://localhost:8080/UserManagement/rest/UserService/users/1 If you hit the above url using your browser or using a java based client or using Postman, result will always be the User XML whose Id is 1 because the server does not store any information about the client. <user> <id>1</id> <name>mahesh</name> <profession>1</profession> </user> Advantages of Statelessness Following are the benefits of statelessness in RESTful Web Services − Web services can treat each method request independently. Web services need not maintain the client”s previous interactions. It simplifies the application design. As HTTP is itself a statelessness protocol, RESTful Web Services work seamlessly with the HTTP protocols. Disadvantages of Statelessness Following are the disadvantages of statelessness in RESTful Web Services − Web services need to get extra information in each request and then interpret to get the client”s state in case the client interactions are to be taken care of. Print Page Previous Next Advertisements ”;
RESTful – Useful Resources
RESTful Web Services – Useful Resources ”; Previous Next The following resources contain additional information on RESTful Web Services. Please use them to get more in-depth knowledge on this. Useful Video Courses Java Web Services Course: Restful API Best Seller 72 Lectures 10 hours Chaand Sheikh More Detail Develop RESTful Java Web Services using JAX-RS and Jersey 28 Lectures 2 hours Vinod Kumar Kayartaya More Detail AWS Application Integration Services Course 15 Lectures 1 hours Harshit Srivastava More Detail Complete Amazon Web Services Course Bundle 146 Lectures 13.5 hours Pranjal Srivastava, Harshit Srivastava More Detail Amazon Web Services – LAMP Setup: Step By Step Course Featured 18 Lectures 1 hours Skillbakery More Detail WCF Demystified: A Primary Course on WCF Services & Web API 10 Lectures 48 mins TELCOMA Global More Detail Print Page Previous Next Advertisements ”;
RESTful – Java (JAX-RS)
RESTful Web Services – Java (JAX-RS) ”; Previous Next JAX-RS stands for JAVA API for RESTful Web Services. JAX-RS is a JAVA based programming language API and specification to provide support for created RESTful Web Services. Its 2.0 version was released on the 24th May 2013. JAX-RS uses annotations available from Java SE 5 to simplify the development of JAVA based web services creation and deployment. It also provides supports for creating clients for RESTful Web Services. Specifications Following are the most commonly used annotations to map a resource as a web service resource. Sr.No. Annotation & Description 1 @Path Relative path of the resource class/method. 2 @GET HTTP Get request, used to fetch resource. 3 @PUT HTTP PUT request, used to update resource. 4 @POST HTTP POST request, used to create a new resource. 5 @DELETE HTTP DELETE request, used to delete resource. 6 @HEAD HTTP HEAD request, used to get status of method availability. 7 @Produces States the HTTP Response generated by web service. For example, APPLICATION/XML, TEXT/HTML, APPLICATION/JSON etc. 8 @Consumes States the HTTP Request type. For example, application/x-www-formurlencoded to accept form data in HTTP body during POST request. 9 @PathParam Binds the parameter passed to the method to a value in path. 10 @QueryParam Binds the parameter passed to method to a query parameter in the path. 11 @MatrixParam Binds the parameter passed to the method to a HTTP matrix parameter in path. 12 @HeaderParam Binds the parameter passed to the method to a HTTP header. 13 @CookieParam Binds the parameter passed to the method to a Cookie. 14 @FormParam Binds the parameter passed to the method to a form value. 15 @DefaultValue Assigns a default value to a parameter passed to the method. 16 @Context Context of the resource. For example, HTTPRequest as a context. Note − We have used Jersey, a reference implementation of JAX-RS 2.0 by Oracle, in the RESTful Web Services – First Application and RESTful Web Services – Methods chapters. Print Page Previous Next Advertisements ”;
RESTful – Quick Guide
RESTful Web Services – Quick Guide ”; Previous Next RESTful Web Services – Introduction What is REST architecture? REST stands for REpresentational State Transfer. REST is web standards based architecture and uses HTTP Protocol. It revolves around resource where every component is a resource and a resource is accessed by a common interface using HTTP standard methods. REST was first introduced by Roy Fielding in 2000. In REST architecture, a REST Server simply provides access to resources and REST client accesses and modifies the resources. Here each resource is identified by URIs/ global IDs. REST uses various representation to represent a resource like text, JSON, XML. JSON is the most popular one. HTTP methods Following four HTTP methods are commonly used in REST based architecture. GET − Provides a read only access to a resource. POST − Used to create a new resource. DELETE − Used to remove a resource. PUT − Used to update a existing resource or create a new resource. Introduction to RESTFul web services A web service is a collection of open protocols and standards used for exchanging data between applications or systems. Software applications written in various programming languages and running on various platforms can use web services to exchange data over computer networks like the Internet in a manner similar to inter-process communication on a single computer. This interoperability (e.g., between Java and Python, or Windows and Linux applications) is due to the use of open standards. Web services based on REST Architecture are known as RESTful web services. These webservices uses HTTP methods to implement the concept of REST architecture. A RESTful web service usually defines a URI, Uniform Resource Identifier a service, provides resource representation such as JSON and set of HTTP Methods. Creating RESTFul Webservice In next chapters, we”ll create a webservice say user management with following functionalities − Sr.No. URI HTTP Method POST body Result 1 /UserService/users GET empty Show list of all the users. 2 /UserService/addUser POST JSON String Add details of new user. 3 /UserService/getUser/:id GET empty Show details of a user. RESTful Web Services – Environment Setup This tutorial will guide you on how to prepare a development environment to start your work with Jersey Framework to create RESTful Web Services. Jersey framework implements JAX-RS 2.0 API, which is a standard specification to create RESTful Web Services. This tutorial will also teach you how to setup JDK, Tomcat and Eclipse on your machine before you the Jersey Framework is setup. Setup Java Development Kit (JDK) You can download the latest version of SDK from Oracle”s Java site − Java SE Downloads. You will find the instructions for installing JDK in the downloaded files. Follow the given instructions to install and configure the setup. Finally set the PATH and JAVA_HOME environment variables to refer to the directory that contains Java and Javac, typically java_install_dir/bin and java_install_dir respectively. If you are running Windows and installed the JDK in C:jdk1.7.0_75, you would have to put the following line in your C:autoexec.bat file. set PATH = C:jdk1.7.0_75bin;%PATH% set JAVA_HOME = C:jdk1.7.0_75 Alternatively, on Windows NT/2000/XP, you could also right-click on My Computer → select Properties → then Advanced → then Environment Variables. Then, you would update the PATH value and press the OK button. On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk1.7.0_75 and you use the C Shell, you would put the following into your .cshrc file. setenv PATH /usr/local/jdk1.7.0_75/bin:$PATH setenv JAVA_HOME /usr/local/jdk1.7.0_75 Alternatively, if you use an Integrated Development Environment (IDE) like Borland JBuilder, Eclipse, IntelliJ IDEA, or Sun ONE Studio, compile and run a simple program to confirm that the IDE knows where you installed Java, otherwise do proper setup as given document of the IDE. Setup Eclipse IDE All the examples in this tutorial have been written using the Eclipse IDE. So, I would suggest you should have the latest version of Eclipse installed on your machine. To install Eclipse IDE, download the latest Eclipse binaries from https://www.eclipse.org/downloads/. Once you downloaded the installation, unpack the binary distribution to a convenient location. For example, in C:eclipse on windows, or /usr/local/eclipse on Linux/Unix and finally set the PATH variable appropriately. Eclipse can be started by executing the following commands on a windows machine, or you can simply double click on eclipse.exe. %C:eclipseeclipse.exe Eclipse can be started by executing the following commands on Unix (Solaris, Linux, etc.) machine − $/usr/local/eclipse/eclipse After a successful startup, if everything is fine, then your screen should display the following result − Setup Jersey Framework Libraries Now, if everything is fine, then you can proceed to setup the Jersey framework. Following are a few simple steps to download and install the framework on your machine. Make a choice whether you want to install Jersey on Windows, or Unix and then proceed to the next step to download the .zip file for windows and then the .tz file for Unix. Download the latest version of Jersey framework binaries from the following link – https://jersey.java.net/download.html. At the time of writing this tutorial, I downloaded jaxrs-ri-2.17.zip on my Windows machine and when you unzip the downloaded file it will give you the directory structure inside E:jaxrs-ri-2.17jaxrs-ri as shown in the following screenshot. You will find all the Jersey libraries in the directories C:jaxrs-ri-2.17jaxrs-rilib and dependencies in C:jaxrs-ri-2.17jaxrs-riext. Make sure you set your CLASSPATH variable on this directory properly otherwise you will face problem while running your application. If you are using Eclipse, then it is not required to set the CLASSPATH because all the settings will be done through Eclipse. Setup Apache Tomcat You can download the latest version of Tomcat from https://tomcat.apache.org/. Once you downloaded the installation, unpack the binary distribution into a convenient location. For example in C:apache-tomcat-7.0.59 on windows, or /usr/local/apache-tomcat-7.0.59 on Linux/Unix and set CATALINA_HOME environment variable pointing to the installation locations. Tomcat can be started by executing the following commands on a windows machine, or you can simply double click on startup.bat. %CATALINA_HOME%binstartup.bat or C:apache-tomcat-7.0.59binstartup.bat Tomcat
RESTful Questions and Answers ”; Previous Next RESTful Questions and Answers has been designed with a special intention of helping students and professionals preparing for various Certification Exams and Job Interviews. This section provides a useful collection of sample Interview Questions and Multiple Choice Questions (MCQs) and their answers with appropriate explanations. SN Question/Answers Type 1 RESTful Interview Questions This section provides a huge collection of RESTful Interview Questions with their answers hidden in a box to challenge you to have a go at them before discovering the correct answer. 2 RESTful Online Quiz This section provides a great collection of RESTful Multiple Choice Questions (MCQs) on a single page along with their correct answers and explanation. If you select the right option, it turns green; else red. 3 RESTful Online Test If you are preparing to appear for a Java and RESTful Framework related certification exam, then this section is a must for you. This section simulates a real online test along with a given timer which challenges you to complete the test within a given time-frame. Finally you can check your overall test score and how you fared among millions of other candidates who attended this online test. 4 RESTful Mock Test This section provides various mock tests that you can download at your local machine and solve offline. Every mock test is supplied with a mock test key to let you verify the final score and grade yourself. Print Page Previous Next Advertisements ”;
RESTful – Methods
RESTful Web Services – Methods ”; Previous Next As we have discussed so far that RESTful web service makes heavy uses of HTTP verbs to determine the operation to be carried out on the specified resource(s). Following table states the examples of common use of HTTP Verbs. HTTP Method GET URI http://localhost:8080/UserManagement/rest/UserService/users Operation Get list of users Operation Type Read Only HTTP Method GET URI http://localhost:8080/UserManagement/rest/UserService/users/1 Operation Get user of Id 1 Operation Type Read Only HTTP Method POST URI http://localhost:8080/UserManagement/rest/UserService/users/2 Operation Insert user with Id 2 Operation Type Non-Idempotent HTTP Method PUT URI http://localhost:8080/UserManagement/rest/UserService/users/2 Operation Update User with Id 2 Operation Type N/A HTTP Method DELETE URI http://localhost:8080/UserManagement/rest/UserService/users/1 Operation Delete User with Id 1 Operation Type Idempotent HTTP Method OPTIONS URI http://localhost:8080/UserManagement/rest/UserService/users Operation List the supported operations in web service Operation Type Read Only HTTP Method HEAD URI http://localhost:8080/UserManagement/rest/UserService/users Operation Returns only HTTP Header, no Body Operation Type Read Only Here are important points to be considered: GET operations are read only and are safe. PUT and DELETE operations are idempotent means their result will always same no matter how many times these operations are invoked. PUT and POST operation are nearly same with the difference lying only in the result where PUT operation is idempotent and POST operation can cause different result. Example Let”s update Example created in RESTful Web Services – First Application tutorial to create a Web service which can perform CRUD (Create, Read, Update, Delete) operations. For simplicity, we”ve used a file I/O to replace Database operations. Update UserService.java, User.java,UserDao.java files under the com.tutorialspoint package. User.java package com.tutorialspoint; import java.io.Serializable; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = “user”) public class User implements Serializable { private static final long serialVersionUID = 1L; private int id; private String name; private String profession; public User(){} public User(int id, String name, String profession){ this.id = id; this.name = name; this.profession = profession; } public int getId() { return id; } @XmlElement public void setId(int id) { this.id = id; } public String getName() { return name; } @XmlElement public void setName(String name) { this.name = name; } public String getProfession() { return profession; } @XmlElement public void setProfession(String profession) { this.profession = profession; } @Override public boolean equals(Object object){ if(object == null){ return false; }else if(!(object instanceof User)){ return false; }else { User user = (User)object; if(id == user.getId() && name.equals(user.getName()) && profession.equals(user.getProfession()) ){ return true; } } return false; } } UserDao.java package com.tutorialspoint; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.List; public class UserDao { public List<User> getAllUsers(){ List<User> userList = null; try { File file = new File(“Users.dat”); if (!file.exists()) { User user = new User(1, “Mahesh”, “Teacher”); userList = new ArrayList<User>(); userList.add(user); saveUserList(userList); } else{ FileInputStream fis = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(fis); userList = (List<User>) ois.readObject(); ois.close(); } } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return userList; } public User getUser(int id){ List<User> users = getAllUsers(); for(User user: users){ if(user.getId() == id){ return user; } } return null; } public int addUser(User pUser){ List<User> userList = getAllUsers(); boolean userExists = false; for(User user: userList){ if(user.getId() == pUser.getId()){ userExists = true; break; } } if(!userExists){ userList.add(pUser); saveUserList(userList); return 1; } return 0; } public int updateUser(User pUser){ List<User> userList = getAllUsers(); for(User user: userList){ if(user.getId() == pUser.getId()){ int index = userList.indexOf(user); userList.set(index, pUser); saveUserList(userList); return 1; } } return 0; } public int deleteUser(int id){ List<User> userList = getAllUsers(); for(User user: userList){ if(user.getId() == id){ int index = userList.indexOf(user); userList.remove(index); saveUserList(userList); return 1; } } return 0; } private void saveUserList(List<User> userList){ try { File file = new File(“Users.dat”); FileOutputStream fos; fos = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(userList); oos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } UserService.java package com.tutorialspoint; import java.io.IOException; import java.util.List; import javax.servlet.http.HttpServletResponse; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.FormParam; import javax.ws.rs.GET; import javax.ws.rs.OPTIONS; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; @Path(“/UserService”) public class UserService { UserDao userDao = new UserDao(); private static final String SUCCESS_RESULT=”<result>success</result>”; private static final String FAILURE_RESULT=”<result>failure</result>”; @GET @Path(“/users”) @Produces(MediaType.APPLICATION_XML) public List<User> getUsers(){ return userDao.getAllUsers(); } @GET @Path(“/users/{userid}”) @Produces(MediaType.APPLICATION_XML) public User getUser(@PathParam(“userid”) int userid){ return userDao.getUser(userid); } @POST @Path(“/users”) @Produces(MediaType.APPLICATION_XML) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String createUser(@FormParam(“id”) int id, @FormParam(“name”) String name, @FormParam(“profession”) String profession, @Context HttpServletResponse servletResponse) throws IOException{ User user = new User(id, name, profession); int result = userDao.addUser(user); if(result == 1){ return SUCCESS_RESULT; } return FAILURE_RESULT; } @PUT @Path(“/users”) @Produces(MediaType.APPLICATION_XML) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String updateUser(@FormParam(“id”) int id, @FormParam(“name”) String name, @FormParam(“profession”) String profession, @Context HttpServletResponse servletResponse) throws IOException{ User user = new User(id, name, profession); int result = userDao.updateUser(user); if(result == 1){ return SUCCESS_RESULT; } return FAILURE_RESULT; } @DELETE @Path(“/users/{userid}”) @Produces(MediaType.APPLICATION_XML) public String deleteUser(@PathParam(“userid”) int userid){ int result = userDao.deleteUser(userid); if(result == 1){
RESTful – Discussion
Discuss RESTful Web Services ”; Previous Next RESTful Web Services are basically REST Architecture based Web Services. In REST Architecture everything is a resource. RESTful web services are light weight, highly scalable and maintainable and are very commonly used to create APIs for web-based applications. This tutorial will teach you the basics of RESTful Web Services and contains chapters discussing all the basic components of RESTful Web Services with suitable examples. Print Page Previous Next Advertisements ”;