0% found this document useful (0 votes)
18 views4 pages

16 To 18

The document contains three experiments demonstrating the use of Spring and Spring Boot. Experiment 16 shows a simple Spring Boot application that returns a greeting message, Experiment 17 creates a REST service for an education site with CRUD operations for courses, and Experiment 18 outlines a web application that returns a greeting message when accessed. Each experiment includes code snippets and instructions for running the applications.
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 views4 pages

16 To 18

The document contains three experiments demonstrating the use of Spring and Spring Boot. Experiment 16 shows a simple Spring Boot application that returns a greeting message, Experiment 17 creates a REST service for an education site with CRUD operations for courses, and Experiment 18 outlines a web application that returns a greeting message when accessed. Each experiment includes code snippets and instructions for running the applications.
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/ 4

EXPERIMENT-16

Write program to demonstrate the concept of spring and spring boot.

// Simple POJO (Plain Old Java Object) class Greeting { private String message; public
Greeting(String message) { this.message = message;
}
public String getMessage() { return message;
}
}

// Spring Component
@Component
class GreetingService { public Greeting getGreeting() { return new Greeting("Hello,
Spring!");
}
}

// Spring Boot Application @SpringBootApplication public class DemoApplication { public


static void main(String[] args) {
// Run the Spring Boot application
SpringApplication.run(DemoApplication.class, args);

// Get the application context


ApplicationContext context = SpringApplication.run(DemoApplication.class, args);

// Get the GreetingService bean from the context


GreetingService greetingService = context.getBean(GreetingService.class);

// Get the greeting message


Greeting greeting = greetingService.getGreeting();

// Print the greeting message


System.out.println(greeting.getMessage());
}
}
OUTPUT :
EXPERIMENT-17
Create REST Service for an Education Site.

package com.education.site; import org.springframework.boot.SpringApplication; import


org.springframework.boot.autoconfigure.SpringBootApplication; import
org.springframework.web.bind.annotation.*; import java.util.*; import
java.util.concurrent.atomic.AtomicLong;
@SpringBootApplication
@RestController
@RequestMapping("/api/courses")
public class EducationSiteApplication { public static void main(String[] args) {
SpringApplication.run(EducationSiteApplication.class, args);
}
// ===== In-memory storage and ID generator ===== private Map<Long, Course>
courseRepo = new HashMap<>(); private AtomicLong idCounter = new AtomicLong();
// ===== CRUD Endpoints =====
@GetMapping public Collection<Course> getAllCourses() { return
courseRepo.values();
}
@GetMapping("/{id}") public Course getCourseById(@PathVariable Long id) { return
courseRepo.get(id);
}

@PostMapping public Course addCourse(@RequestBody Course course) { long id =


idCounter.incrementAndGet(); course.setId(id); courseRepo.put(id, course);
return course;
}
@PutMapping("/{id}") public Course updateCourse(@PathVariable Long id,
@RequestBody Course course) {
course.setId(id); courseRepo.put(id, course); return course;
}
@DeleteMapping("/{id}") public void deleteCourse(@PathVariable Long id) {
courseRepo.remove(id);
}
// ===== Course POJO =====
static class Course { private Long id; private String title; private String
description;

public Long getId() { return id; } public void setId(Long id) { this.id = id; }

public String getTitle() { return title; } public void setTitle(String title) { this.title = title;
}

public String getDescription() { return description; } public void setDescription(String


description) { this.description = description; }
}
}
EXPERIMENT-18
Use the Spring Boot Starter Web to Create a Web Application.

HelloWebApplication.java package com.


example.helloweb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.
SpringBootApplication; importorg.springframework.web.bind.annotation.*;

@SpringBootApplication @RestController public class HelloWebApplication {

public static void main(String[] args) {


SpringApplication.run(HelloWebApplication.class, args);
}

@GetMapping("/")
public String home() { return "Hello, Spring Boot Web!";
}
}

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>helloweb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>

How to Run
Place the Java file in:
src/main/java/com/example/helloweb/HelloWebApplication.java
Use this command:
bash CopyEdit mvn spring-boot:run

Output :
After running, visit:
arduino CopyEdit http://localhost:8080/
✅ output
Hello, Spring Boot Web!

You might also like