Join Ketkee Aryamane for an in-depth discussion in this video Setup for a JAX-RS application, part of Java EE 8: JSON-B.
- [Narrator] So that is all that we have to see under the JSON binding API we saw the default settings a whole lot of settings there, then we saw a lot of custom options, both ways, the compile time and the runtime customizations. And then we also saw how to have your own serialization by means of adapters and serializers. The last bit of this course is where we are going to see how do you get the JSON binding API and put it inside a JAX-RS which is Java API for restful web service application.
So we are going to build a very small JAX-RS application quickly and make sure that we use JSON binding in order to send and receive Java JSON object and vice versa. Okay. So let's set up that application first. It's very simple to do that. And it means that we have create a Maven project from the Java EE 8 archetype. So let's go and do that. You say right click, new project, say Maven and choose this option project from archetype and what are we gonna choose it's a restful web application that we want so, Java EE 8 essential archetype should be good.
Say next, give it a name so let's say JSONbWeb. Let's get rid of the default package we don't want this package. We'll create our own later when the structure of the project gets generated. Okay say finish and let's get the project structure generated first. Okay so I'm going to minimize the earlier one and that's the one that we want, okay. Now under source packages there are some default packages that get generated out of that archetype.
We don't want them. What we'll do is we'll create our own packages so com.app.rest so this is the folder that is going to contain all the rest classes. Then another source package for all the beans that we use. And then we'll also create one package for the utis. Okay. Let's understand this quickly. This is a JAX-RS application, right? So you will have a basic class which is the basic configuration class for JAX-RS which extends the application class.
Let me tell you that it has already been generated here under the com.airhacks package. Okay if I just open this that's what it looks like. It extends the application class. It is annotated with the applicationpath annotation and that is what specifies the basic URI for this application. So if you want to access any other restful bean in this application, you'll have to first come to this part and then access the part of the respective restful class. Okay. So we'll just get this, copy this and put it in a rest package.
Paste it there along with the refractor option. Once we do this, now we can completely get rid of these packages that were created by default. Okay so I'm going to delete them and I'm gonna remove all references. Okay we are good. So we've created the rest package, we've created the bean package, and we've created the utis package. Now, the magical thing is that under the poem xml, which is the Maven configuration file for this project, do you see a dependency for Java EE API? Yes.
So now that it is already there, we do not need to really add the JSON bind API dependency separately. The entire dependency has already been added to our project so it's right out there available with us. Okay. So that was the first step create a Maven project from the archetype. Next is add GlassFish Server 5 to the IDE. Now we are gonna download GlassFish Server 5 as a part of the setup and let's say you don't know how to add a server. I'll show you that. Let's go to tools, servers, and here you have an option to add server.
So when you click on add server, you can choose the option of GlassFish server. Say next. You can also give it a name by the way, down there. Say next and then you need to feed the path of the GlassFish instillation data tree. When you download, it comes as a zip package. You just need to go to the home part of GlassFish server and then feed that path to this instillation location. So let me show you the GlassFish instillation path. Let's go to the explorer under my D drive softwares, I have GlassFish already unzipped when I downloaded.
So I can go there and that becomes the path of my GlassFish server. Okay, you just have to take this and paste it right there in your IDE and then you're done. It creates a local domain for you and when you say next it will install that server for you. Now, for me, in this netsbean IDE setup I already have that available with me so I'm not going to go ahead with this operation. I'm just going to cancel this but, these are the definite steps to get the GlassFish server added to netbeans. Okay, we need this server because we need to deploy this restful application on the server.
Alright. So, that is done too. We have created the package for beans and rest classes. Yes we have done that. The rest of the stuff we will see in the next video.
- JSON and JSON-B API basics
- Default mapping
- Defining beans and data
- Customized mapping
- Customizing the order of serialized properties
- Customizing constructors
- Working with binary data
- Custom serializers and deserializers
- Demo: Using JSON-B in a web application