0% found this document useful (0 votes)
18 views

Annotations in Java-Spring

This document discusses annotations in Java and creating RESTful web services using Spring Boot. It covers: - Annotations provide metadata about a program that is not part of the code itself and have no direct effect on operation. - The @SpringBootApplication and @ComponentScan annotations configure the Spring Boot application. - RESTful web services are built using the @RestController and @GetMapping annotations to handle HTTP requests and return responses. - A sample RESTful web service is created that returns a string when requesting the /hello path. Exercises include adding additional endpoints.

Uploaded by

mihaela cucos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Annotations in Java-Spring

This document discusses annotations in Java and creating RESTful web services using Spring Boot. It covers: - Annotations provide metadata about a program that is not part of the code itself and have no direct effect on operation. - The @SpringBootApplication and @ComponentScan annotations configure the Spring Boot application. - RESTful web services are built using the @RestController and @GetMapping annotations to handle HTTP requests and return responses. - A sample RESTful web service is created that returns a string when requesting the /hello path. Exercises include adding additional endpoints.

Uploaded by

mihaela cucos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Cloud Based Web

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

• Last week in the code generated by SPRING we got


• @SpringBootApplication
Annotations in Java
• This “Tags” are called annotations

• They are “a form of metadata, provide data about a program that is


not part of the program itself. Annotations have no direct effect on
the operation of the code they annotate.”

• 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

• Add a new annotation after the first one


• @ComponentScan(“ie.cct”)

• Notice that “ie.cct” can change depending on the project your


working on
Our Second Annotation
• This annotation is telling the compiler that there are other folders
that need to be scanned as they also have some important code

• Spring is going to be able to find all of the meta data (other


annotations) defined around.

• It is going to do an index of the different possible path that can be


followed
Before we move on…
• Remember your HTTP codes from last week?
• Make sure you know them. Not all of them, but at least the categories

• HTTP has also different methods


• GET
• DELETE
• POST
• UPDATE
• PATCH
GET Method
• Every time you open a web page, your browser is performing a GET
request to a particular URL.

• Do a quick research of what other methods are available in the HTTP


protocol
• Make sure you know the important ones
What does it have to do with us?
• Remember the architecture I showed you last week?
• Clients are going to connecting to those microservises through GET
requests on HTTP

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

• Add a new file in the new folder called “MyFirstController.java”


The code
package ie.cct.controllers;

public class MyFirstController{

• Notice the package declaration. Like in any Java program we need to


specify in what folder this code lives.

• If you don’t quite get this. Let me know!


Let’s add some fun
• The code highlighted in yellow
package com.example.controllers; is what we’ll add
import org.springframework.web.bind.annotation.GetMapping; • An annotation to tell the
import org.springframework.web.bind.annotation.RestController; compiler this is the REST
@RestController controller
public class HelloController{
• A method to handle requests to
@GetMapping("/hello") http://my.domain.com/hello
public String hello(){
return "Hello CCT";
}

} • The code highlighted in green


should be added
automatically by the editor
Try it!
• Once you have the code, run the program, go to your web browser
and open
• http://localhost:8080/hello

• 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

• It is a software architectural style that defines a set of rules for web


services.
• The most APIs now a days are RESTful.
REST
• What we are doing here is to write a RESTful API for our microservices

• Do a quick research on what are the characteristics of a REST (or


RESTful) API
Exercise
• Create another method for http://localhost:8080/my/name that
returns your name
That’s all folks
• Any questions?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy