Weather display based on the city using springboot and Rest API
Weather display based on the city we are giving in the text field using springboot and Rest API https://openweathermap.org/ Have to sign up(create one account get api) in the website: https://home.openweathermap.org/users/sign_up 3.They will provide api key 4.API have data in JSON format -(JSON- JavaScript Object Notation ) In API data are in map (Key: value format) Project Creation: Steps: Create new Spring Starter Project - WeatherApp Dependencies - Springboot DevTools, Spring Web, Thymeleaf,lombok 3.Create below packages com.example.demo.model com.example.demo.controller com.example.demo.service 4.JackSON Data Bind Springboot don’t have dependency so get the dependency from mvnrepository.com. Add below dependency in POM.xml: com.fasterxml.jackson.core jackson-databind Why dependency needed here? We have to get value from other website (openweathermap) this website has data in JSON format. Springboot doesn’t know how to read JSON data. So we need JackSON Jar file in springboot. We are going to store those data in Model(WeatherResult) class 5.Create one POJO(Model or Entity) class in Model Package Create the fields as private whatever we need to display in browser. Add @JsonIgnoreProperties(ignoreUnknown = true) in the Model class i used city, temperature , humidity and description fields from JSON. Created getter and setter, toString(), NoArgsConstructor,AllArgsConstructor - we can use Lombok here. 6.Create separate package for Service and create service class , add business logic here. Here logic is `@Service public class WeatherService { @Value("${apikey}") private String api_key; private String api_url="https://api.openweathermap.org/data/2.5/weather?q={city}&appid={apiKey}"; public WeatherModel getdetails(String city) { RestTemplate restTemplate=new RestTemplate(); return restTemplate.getForObject(api_url,WeatherModel.class,city,api_key ); }` REST API stands for REpresentational State Transfer API. It is a type of API (Application Programming Interface) that allows communication between different systems over the internet. REST APIs work by sending requests and receiving responses, typically in JSON format, between the client and server. REST APIs use HTTP methods (such as GET, POST, PUT, DELETE) to define actions that can be performed on resources. These methods align with CRUD (Create, Read, Update, Delete) operations, which are used to manipulate resources over the web. (Send HTTP Requests) (api URL) client----------------------------->Server Getting JSON Response A request is sent from the client to the server via a web URL, using one of the HTTP methods. The server then responds with the requested resource, which could be HTML, XML, Image, or JSON, with JSON being the most commonly used format for modern web services. Here RestTemplate class is used to get data as object from REST API To interact with REST, the client needs to create a client instance and request object Request Resource Spring Frame work ---->REST TEMPLATE---> REST API Get a Resource We have to convert JSON object to POJO(Model) class object. RestTemplate class is used to get value from JSON. Create RestController class . Using @Autowired Service class and Controller class communicates dependency is injected. `@RestController() @RequestMapping("/weather") public class WeatherController { @Autowired private WeatherService weatherService; @GetMapping("/") public String displayWeather() { return "HomeWeather"; } @GetMapping("/weather_result") public String getWeather(@RequestParam String city,Model model) { WeatherModel result=weatherService.getdetails(city); model.addAttribute("weatherdetails",result); return "HomeWeather"; }` if the web request matches to GetMapping it renders to the html page. Model class addAttribute() is used to bind values as object and send to frontend These values are displayed using thymeleaf th:text in the browser. Create Homeweather under src/main/resources/template directory Create one form for city and button. Place this code in Homeweather.html Output: Dependency used in this application @Service @Value @RestController @RequestMapping @Autowired @GetMapping() @RequestParam

Weather display based on the city we are giving in the text field using springboot and Rest API
- https://openweathermap.org/
- Have to sign up(create one account get api) in the website: https://home.openweathermap.org/users/sign_up
3.They will provide api key
4.API have data in JSON format -(JSON- JavaScript Object Notation )
- In API data are in map (Key: value format)
-
Project Creation:
Steps:- Create new Spring Starter Project - WeatherApp
- Dependencies - Springboot DevTools, Spring Web, Thymeleaf,lombok 3.Create below packages com.example.demo.model com.example.demo.controller com.example.demo.service
4.JackSON Data Bind
Springboot don’t have dependency so get the dependency from mvnrepository.com.
Add below dependency in POM.xml:
com.fasterxml.jackson.core
jackson-databind
Why dependency needed here?
We have to get value from other website (openweathermap) this website has data in JSON format. Springboot doesn’t know how to read JSON data. So we need JackSON Jar file in springboot. We are going to store those data in Model(WeatherResult) class5.Create one POJO(Model or Entity) class in Model Package
Create the fields as private whatever we need to display in browser.
Add @JsonIgnoreProperties(ignoreUnknown = true) in the Model class
i used city, temperature , humidity and description fields from JSON.
Created getter and setter, toString(), NoArgsConstructor,AllArgsConstructor - we can use Lombok here.6.Create separate package for Service and create service class , add business logic here.
Here logic is`@Service
public class WeatherService {
@Value("${apikey}")
private String api_key;
private String api_url="https://api.openweathermap.org/data/2.5/weather?q={city}&appid={apiKey}";public WeatherModel getdetails(String city) {
RestTemplate restTemplate=new RestTemplate();
return restTemplate.getForObject(api_url,WeatherModel.class,city,api_key );
}`
REST API stands for REpresentational State Transfer API. It is a type of API (Application Programming Interface) that allows communication between different systems over the internet. REST APIs work by sending requests and receiving responses, typically in JSON format, between the client and server.
REST APIs use HTTP methods (such as GET, POST, PUT, DELETE) to define actions that can be performed on resources. These methods align with CRUD (Create, Read, Update, Delete) operations, which are used to manipulate resources over the web.
(Send HTTP Requests)
(api URL)
client----------------------------->Server
Getting JSON Response
A request is sent from the client to the server via a web URL, using one of the HTTP methods. The server then responds with the requested resource, which could be HTML, XML, Image, or JSON, with JSON being the most commonly used format for modern web services.
Here RestTemplate class is used to get data as object from REST API
To interact with REST, the client needs to create a client instance and request object
Request Resource
Spring Frame work ---->REST TEMPLATE---> REST API
Get a Resource
We have to convert JSON object to POJO(Model) class object.
RestTemplate class is used to get value from JSON.
-
Create RestController class . Using @Autowired Service class and Controller class communicates dependency is injected.
`@RestController()
@RequestMapping("/weather")public class WeatherController {
@Autowired
private WeatherService weatherService;
@GetMapping("/")public String displayWeather() { return "HomeWeather"; } @GetMapping("/weather_result") public String getWeather(@RequestParam String city,Model model) { WeatherModel result=weatherService.getdetails(city); model.addAttribute("weatherdetails",result); return "HomeWeather"; }` if the web request matches to GetMapping it renders to the html page. Model class addAttribute() is used to bind values as object and send to frontend These values are displayed using thymeleaf th:text in the browser.
Create Homeweather under src/main/resources/template directory
Create one form for city and button.
Place this code in Homeweather.html
Dependency used in this application
@Service
@Value
@RestController
@RequestMapping
@Autowired
@GetMapping()
@RequestParam