My First Spring API

     If you read my last post, you'll know I had all sorts of troubles getting my Spring API connected to my Postgres server, but that was by far the most arduous part of my journey with Spring. The application only has simple CRUD commands right now, but that will be more than enough for me to later use this API when I start learning Angular. 

    Anyway, it's easy to get started with the Spring Initializer at start.spring.io. It's a simple form that lets you put in some basic information and then download a zipped file with your new project. From there, I just added a Models, Controllers and Repositories folder (and did all that stuff to get connected to my database, but you can check that out here). Once I had created files for the different speakers and conference sessions (the data is all from a PluralSight conference or something), my file structure looked like this:

     The biggest takeaways I had were on setting up the many-to-many relationship, handling serialization and the request mapping.

Many-to-Many Relationships: The magic here was to simply create in my sessions model a list of speakers, and about it give the @ManyToMany notation and the @JoinTable notation to specify the ID's of the tables that will be used in the join.


Handling Serialization: Initially my get request pulled an entry from a session, which had a speaker, which had a session, which had a speaker, and so on. In order to prevent this, I just had to throw @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) onto the top of my model, easy peasy. This just ignores the additional handles, so when I grab sessions it grabs speakers, but doesn't bother going back up to tell me the session of that speaker.


Request Mapping: This was simply the different ways we would allow the API to be called, at the top of the controller we needed to specify the path ("/api/v1/sessions") and each call would need some its own mapping. Some, like displaying all the sessions, just used a simple Get request, so there was no additional request mapping, but @GetMapping was specified. Getting a session by an ID had the request mapping of ("{id}"). Create and Delete were the most interesting, as create required a @PostMapping notation and delete had to specify the RequestMethod.

    You can see the full code here.

Comments

  1. When the cube have been hot, craps was a party, and once 코인카지노 they weren’t, everybody suffered together. Blackjack, in contrast, is a sport that pits each participant individually towards the dealer and sometimes each other. If the dealer's up card is an Ace, ask if anyone wants insurance coverage.Players might place a separate "insurance coverage" bet.

    ReplyDelete

Post a Comment

Popular posts from this blog

API's in C#

Using WebRTC to build a videophone in React and TypeScript

Reviewing WPF and MVVM