How to read JSON data in Spring Boot and write to a database

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this lesson we're going to take a look at a question from a student in my spring boot introduction course if you'd like to take this course for 50% off please use the link below with that let's get to the question so the question is simply this the student has a regular web MBC application a spring boot web app and they have some JSON data and the question is how can I take this JSON data and insert it into the DB so well that's what exactly what we're going to do today we're going to start from scratch create a new spring boot project we'll grab some dummy JSON data from one of my favorite services and we'll take that data and insert it into an h2 database so let's go ahead and get started so as I said there is this website called JSON placeholder which I really love and it gives us a nice easy way to grab some kind of fake dummy data and so today we're going to use these users and those users are just 10 users here and they're made up of a bunch of data and so with that let's go ahead and create our project so I'm going to create a new project and we're going to use Java 8 spring initializer let's say comm dot the real Dan Vega we're going to call this JSON DB we'll use maven jar Java and we'll just go on from there so let's take a look at what we might want I always start with dev tools I use web as a dependency here nothing else there for sequel as I said we're going to use J PA and the h2 database and I think for ops I don't think we need anything let's just go ahead and throw that in oh I do need I wanted something else here how about Lombok always makes my life easier I love Lombok let's go ahead and in next let's go ahead and throw this in boot JSON DB and we'll finish okay create alright so why that's firing up here let's go back here I'm going to grab this data and inside of here where I want to put my data is in source main resources I'm just going to create a new folder here so we'll say new directory we'll just call it JSON and we're going to say new file users dot JSON let's go ahead and paste that in alright so the next thing we want to do is we have some properties let me go ahead and just copy this I'm going to copy some stuff here set up the sitter and watch every little thing you typed out so there's our properties we're basically enabling our h2 to council's so we can look at that we're changing the data source name to users and we're enabling those endpoints if we need them so there's our data now what we need to do is actually start building out our project so what I'm going to do is come in here start here with lien and let's just start to build this out so we're going to need a new couple packages so I'm going to go package controller go back up command n start typing package just a little shortcut to get started with everything so I have a domain I have a package repository and then backup package service and that should do us ok so let's just start this one by one we're going to have a user controller so let's start with Java class we'll call this user controller and inside of our controller here this is going to be a rest controller we're going to have a request mapping of slash users all right so let's start there and then I like to kind of build my applications out from from my starting point so I'm going to have a user controller but I know that I don't want to put business logic in here so what I end up doing is creating a service most of the time I create a interface and an implementation today we'll just keep it short just for demo purposes so what I do is I start here and say all right well I'm going to need a user service and it's going to be called user service but as we can see we don't have a type for user service so if we come in here we can create a class called user service and we can tell where it's going to go so in this case it's going to go in our service package and we'll go ahead and hit OK now in my go back here so now that we have that I can actually create a constructor so I'm going to create a constructor and inside there we're going to get our user service and finally I'm going to have one more get mapping and this is going to be for a list and what we're going to do here is we're going to get an iterable of users we haven't defined that yet and we'll get a list back and to do that we're going to use our user service and we're going to use a method called lists so again we're here we already have our user service we could come in here and say create method list and so now creates a method for us inside of our user service class I always forget to use service here so we'll do that so next is our service class so we're going to need we're going to need some stuff in here and to do that we're actually going to need our repositories but before we get into the repository we're going to actually have to build out our domain model right so let's create a new class and we'll call it user this is inside of our Java class I'm sorry this is inside of our domain package and this is going to represent a user I'm going to go ahead and start copying and pasting some stuff again I know you don't want to watch me type all this so let's go ahead and copy this in and we'll just talk this way through okay so this is in 2d class this is Java X okay so we're going to start talking about this so let's jump into our back to our JSON data so inside of here we have an ID name username email and then we have this address block which is basically another object inside of our object now we could kind of make this a relational schema but I think to keep this simple what we're going to just do is kind of have everything embedded and embeddable so we'll create a new domain object called address which is embeddable and we'll embed that right inside of our user domain object and all that does is create one table for all these objects so we'll do the same with company and even inside of address we'll do the same with geo so if we look at that we have all of our properties here and now we have this embedded private address address so if we jump go ahead and copy this as well so inside of our domain we're going to create a new Java class and we'll call this address and we'll just paste this and we should be good there so now again we're going to have the same problem we're going to need a new java class called geo inside of here and let's just make sure we have this right and we'll paste this and that's our geo class if it basically has a latitude and longitude so that's good and now that satisfies our address and one more thing we're going to need a company and I think the company just had a few things named catchphrase and BS I'm sure that is but maybe it is BS all right so let's go one more inside of here and Java class company I realize this is probably a little bit too much for just a simple demo but I wanted to show this just like to grab this data from that website and it's a nice example of a little bit more complex than just a simple user class so we're just going to kind of embed every here and so when we say embeddable here this just makes this class you're not marking this as an entity this isn't something that's creating its own separate table it's just saying that this class is now embeddable and somewhere else so in our user object we have our normal properties we have address which then has an embeddable object itself for geo and then we also have company so that I think satisfies our domain models so now if we go back to our user service we need a repository in here right so just come back here and think all we needed to do you know all right so what we're going to do is we're going to get a private user repository and that's going to be called user repository and will generate a constructor for that as well and so we're going to need to create this one so we come in here and create user repository again we're going to create this in the repository package so there that is the only thing difference here is this is an interface and this actually extends crud repository so what is the type the type of user the ID is of type long so that's our user repository that's Pro it is this is going to be the pool listen import that okay much better looking so that's our repository that's going to give us what we need so now in our user service we have a user repository we can actually pull this in and we need to build out our list method and to do so we're going to what do we need to do here okay so first off this is actually going to take a list of user and we'll call this users and so the user repository can take a list and save also we can save a list of users at once and that's basically going to be the JSON list of data that we get it does not like that and big you just call in credit repository and save whoops okay and import list okay so that will finish off our list method next we're going to need a save method okay so we're going to need a save method here and we'll just say this is going to be public this is going to return a user call it save use your user and this is just going to use the user repository dot save and then that's a user so just notice the type of I'm just kind of doing this really fast here so that's probably why all right so now I have a save method to take one user and I have a save method to take a list of users I don't think I actually need this I think I was doing that for just a single demo but I also want a iterable list of users and I need a list method inside of here we didn't oh that's why I just override at that okay now this is making sense so inside of here we're going to return a user repository it would really help if I could type today we use a repository to find all so that's just going to get us a list of them all right now I feel much better so this is our save we still have something going wrong in here use a repository what is going on in here okay at service private he's a repository that's our service that's injecting that don't mean is never used that's fine aha return return return okay that looks good user service so now back to our user controller this is returning required okay so we got to pass this in so we need to list out why does it take a return list list should not take a user okay so probably just made a simple mistake here so let's look at list required iterable user found iterable calmed did I pull it along okay weird all right so now I ever use a controller this is starting to look good so let's talk about this we have our user controller we can hit slash users slash list and we'll get a list of users we have our different domain objects that really just make up that one single user that's going to get stored in a user table we have a repository for fetching our data and saving it we have a user service that is kind of the in-between our controller and our repository basically where we're going to house our business logic finally we have our main application here and so now here comes the fun part so now we need to basically read in a list of JSON users which is in our JSON users JSON file and then we need to go ahead and write them to database so let's kind of walk through this so first we're going to create a beam and we're going to do a command liner in here and this Runner is going to allow us to do something right when the application starts up and what we're going to do is we're going to inject our user service call it user service and this is a functional interface so it's going to look like this and we're going to read JSON and write to DB okay so the first thing we need is we need some helpers for us and this is actually going to come in the way of Jackson and so if we look in our spring boot pom-pom dot XML here when we pull in the web dependency we actually get some Jackson data binding here and inside of that Jackson data by there's some really good classes to help us map a particular JSON structure to an object and so that's really what we're going to do here and so to do that we're going to get an object mapper so a good object mapper will call this mapper and it's going to be a new object mapper and what we're going to do with this is this type reference class actually let me just go ahead and paste this in you guys probably do not want to watch me type this out so let's talk through this real quick import that looks good list users do we bring the wrong one end yeah don't know why I brought didn't we want okay so here's our object mapper again this is in the Jackson package that we brought in now we have a type reference we're basically saying we want a list of users and that's going to initialize our list of users so we're using an input stream to basically read the JSON file so we're going into the resources folder and grabbing JSON slash users JSON and here we're going to do just a try-catch and we're going to say alright I want to get a list of users will call that users and it's basically going to use that object mapper which helps us map the JSON structure to a domain object so it's going to read the value here's our input stream here's our type reference here's what our type is and then we're just using our user service which calls our user repository to save the users so let's go ahead and run this and see if this works if it does we should see a user saved in the council if not it's going to say unable to save users so as I can tell you when I was working on this demo there were a few times that it just wasn't quit quite working and the reason for it is if that JSON structure doesn't look like that exact model of your domain then you're going to get some errors and there are ways around that you can use kind of ignore JSON ignore and some different things but you can come into some problems and it looks like we already did so no identifiers specified for entity calm invocation of an it method okay so basically what this is saying is that our user we have an ID here strategy is embedded embedded is ID where did that coming from that is our problem so we just brought in the wrong one there let's try this again all right so unable to save users unexpected character may be a non-standard comment on yep all right so we can actually fix this but an easy way to do this is to just get rid of that and let's run it again you you all right so user saved so let's run back to the UI Here I am on localhost 8080 we don't have anything for there so we're going to go to users list and there is all of our users so if we also want to check this out we can go into the h2 Council remember we change our data source name to users so we're going to need to make sure we use that there it is on our GD BC URL we're going to connect to that here's our user table if we run it so again remember we kind of embedded everything into that user object so because of that we just get this one large table but again for demo purposes that was fine just because it made a little bit more sense so I know that wasn't kind of a long way around what was the meat and potatoes of this presentation which was really kind of reading and writing JSON but I just wanted to show you from scratch it's really not that hard we basically build out our application like we normally would and then back in our main application class this is really the fun part that makes everything happen so we're using a command line runner we're using object mapper that's in Jackson so if we go ahead and click on this you can see this comes from the Comcast your XML that Jackson core so that's all part of the mapping a JSON object to a domain object so we're doing that we're reading in that JSON file and then we're just going to map our list of users over to a list of users really save them off and then we can go ahead and present them using our controller or return them using our controller and we can also see that from the h2 console side so thank you for the question I hope this helped walking through the steps of basically reading and writing JSON to a database and I will go ahead and I have this all of the source code up on github I'll paste the link below to that please if you like this give me that thumbs up I like to know that these videos are helping someone out there subscribe to this channel and you know give me some feedback let me know what kind of presentations tutorials that you'd like to see because I'm doing this for you guys and again if these aren't helpful then I need to find something else to do with my time so I hope you enjoyed this and I will see you in the next one friends
Info
Channel: Dan Vega
Views: 69,344
Rating: undefined out of 5
Keywords: Spring Boot, Spring Framework, JSON, Write JSON, JSON to Database, JSON to DB, spring boot json, spring boot with json, nsted json springboot save database, spring, create a rest api using json file in spring boot, spring boot json file, handling json in spring boot, how to get data from json in spring java, store json data in database using springboot, how to listen json file and write in spring, spring boot, spring boot tutorial, java spring boot, json tutorial, json java
Id: rGdKmF2UzSc
Channel Id: undefined
Length: 23min 36sec (1416 seconds)
Published: Tue Jul 04 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.