Annotations in Java-Spring
Annotations in Java-Spring
Applications
Week 5
Amilcar Aponte
amilcar@cct.ie
Last week
• Starting with SPRING
Today’s Plan
• More SPRING
Acknowledgment
• David Gonzalez, who is teaching this module to the part time group,
as the brain behind these slides.
• I just put them together in a nice-ish format
Annotations in Java
• You’ve probably have notice in you java programs that some methods
must include something like
• @Override
• @Test
• https://docs.oracle.com/javase/tutorial/java/annotations/
Annotations in Java
• In other words:
• We can provide the compiler with some information about the methods or
classes
• We can also give the compiler information to be used at compile-time or
deployment-time
• Some annotations are available to be examined at runtime
• https://docs.oracle.com/javase/tutorial/java/annotations/
Our First Annotation
• Notice that the program we used last week has already one annotation
• @SpringBootApplication
• This tag is telling the compiler that this is the main method and the
program starts here. It has also some other implications, but we won’t go
down that road
• https://docs.spring.io/spring-boot/docs/current/reference/html/using-
boot-using-springbootapplication-annotation.html
Our Second Annotation
• Using the same project that we started last week, go to your
FirstProgramApplication.java
HTML / JS HTML / JS
Microservices
Web Browser
Client Server
Handlers
• We need to be able to and handle those requests as they come
• http://my.domain.com/pretty
• http://my.domain.com/ugly
• http://my.domain.com/terribly/adorable
Handlers
• We’ll use two annotations to define what to do when those requests
come along
• @RestController
• This annotation is telling SPRING that the following class has all the
instructions of what to do when the requests come to server
• @GetMapping(“/mylocation”)
• This annotation is telling SPRING that this method is the one that must run
when there is a request on http://my.domain.com/location
Handlers
• Notice that the first one applies to a class and the second one applies
to a method
Finally, let’s code
• Create another folder called “controllers” beside your “firstprogram”
folder
• Do you remember what other ways we did last week to open the
same page?
• Using your phone (connected to the same WIFI)
• Using curl
What is REST?
• You might be wondering now what is REST
• REpresentational
• State
• Transfer