Join Patrick Royal for an in-depth discussion in this video Creating the web service endpoint, part of Building Web Services with Java EE.
In this video, we are going to write the message for the web service endpoint based on the specification from the previous video. I recommend that you follow along and try to write the message as I describe them. But if you prefer, you can download the code from the exercise files for Chapter Five. Let's start by creating a new dynamic web project. We'll call this project MeetingScheduler. Then navigate to the Java resources \ source folder, and create a new package to hold our code.
Finally, in this package, we'll create a class to serve as the root resource class. Let's just call it root. In this class, we'll create the method used to handle the HTTP command, at post. Technically we could put all of the methods for this simple web service in one class, but the preferred coding practice is to separate out HTTP command methods from standard Java methods. For larger and more complex programs, this is helpful to keep things organized. The first thing to add as usual with a REST class is the @Path annotation to indicate that this is a web service, the value here can be any string.
Make sure to import this, if you haven't already. Next, we need to add a constructor. This can be left blank engineered but it is required for the interface. Now let's create the actual method. We'll call it "post". As noted in the specification, the purpose of this method is to take in and date and return success or failure based on rather it has an open slot. So the method should both accept and output a string. We also need to add the REST annotations so add the line @Post above the method.
And then this both consumes and produces a string of plain text. So add the annotations @Consumes text / plain and @Produces text / plain. As always we need to import these. The body of this method references other methods that we haven't created yet, so just leave it blank for now and create a new class for the server side functionality which, I'll call schedule function.
So right-click on the project>New>Class and then just input the name here. This is an ordinary Java class so, rather than typing out all the code, I'm just going to copy and paste it from the exercise files. You can do the same from the exercise files for Chapter Five, Video Two. Then you can just navigate through the final folder. Thesaurus, in the scheduler package and then right-click the Schedulefunction.javafile and choose to edit it with Notepad++ or if you're on a mac, with Textwrangler.
In this file, just select the entire code, right-click, choose Copy. And then go back to Eclipse and select everything and paste. The load and save methods for this file are fairly standard conversions from an array list to a text file. So, they can simply be called by any other function to convert the Java array list to and from a file. For the algorithm itself, it should take in a date as a string. And then it should return a Boolean value, for success or failure.
So write the method as, public static Boolean updateTime(String date). From here we will convert the date to an integer and then call the load method, to get the array list from the file. You can use any file name here, just be sure to create a blank text file at that location on your computer. In this example, I've used a very simple file path C/ database.txt. So, I just made sure to go into my C folder and create a new blank text document called database.
Since we don't know exactly how large to made the array, the simplest thing to do is to let it dynamically update. I added a simple loop that extends the schedule array to be at least as large as the date. Then, we just check if the I-th entry in schedule is equal 0, and if so, update it to 1 on the array. Save the new array back to the file and return true. Otherwise, we return false. Now this class is complete so we can return to the root class and add in a simple check. If ScheduleFunction.updateTime with the given date returns true, then we return the string, Success.
If not, then we return the string, Failure. Now, just go ahead and hover your mouse over the ScheduleFunction call and make sure to add the throws declaration. Within the ScheduleFunction, that method has the ability to throw an IOException, because it's reading from a file. So we just have to extend this exception to the post method. With that, our simple web client is complete. Now let's move on to testing.
- What are Java EE web services?
- Creating a simple service
- Building, packaging, and deploying your service
- Understanding the basic syntax
- Working with WSDL (Web Service Definition Language)
- Exploring SOAP (Simple Object Access Protocol)
- Exploring the syntax and design of REST (Representation State Transfer)
- Programming a web service in Java EE
- Debugging and optimizing your code