0% found this document useful (0 votes)
38 views11 pages

Akshay

The document outlines the creation of a simple Spring Boot application that displays a welcome message and demonstrates RESTful web services with a product management application. It includes code for a message controller, product model, and product controller with CRUD operations. The application can be accessed via HTTP methods at specified endpoints for managing products.

Uploaded by

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

Akshay

The document outlines the creation of a simple Spring Boot application that displays a welcome message and demonstrates RESTful web services with a product management application. It includes code for a message controller, product model, and product controller with CRUD operations. The application can be accessed via HTTP methods at specified endpoints for managing products.

Uploaded by

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

Name: Akshay Roll no:

Sreedhar 03

CO3 (Module 5) : Build Spring Boot application using


RESTful web services.

1. Write a program to create a simple Spring Boot application that


prints a message.

Code:
MessageController.java
package com.FirstSpringApp.FirstSpringApplication;

import
org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class MessageController {
@GetMapping("/message")
public String getMessage(Model m) {
m.addAttribute("message","Hello Akshay welcome to
MCA"); return "message";
}
}

message.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
Name: Akshay Roll no:

<meta http-equiv="X-UA-Compatible" content="ie=edge">


<title>First Spring Application</title>
<h1 th:text="${message}"></h1>
</head>
<body>

</body>
</html>

FirstSpringApplication.java
package com.FirstSpringApp.FirstSpringApplication;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class FirstSpringApplication {

public static void main(String[] args) {


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

}
Name: Akshay Roll no:
Sreedhar 03

Output:

http://localhost:8080/message
Name: Akshay Roll no:

2. Write a program to demonstrate RESTful Web Services


with spring boot.(Product Application)
Code:
Product.java
package com.Product.ProductExample;

public class Product {


String id;
String name;
public String
getId() {
return id;
}
public void setId(String id)
{ this.id = id;
}
public String getName() {
return name;
}
public void setName(String name)
{ this.name = name;
}

ProductController.java
package com.Product.ProductExample;

import java.util.HashMap;
Name: Akshay Roll no:

import java.util.Map;

import
org.springframework.http.HttpStatus;
import
org.springframework.http.ResponseEntity;
import
org.springframework.web.bind.annotation.DeleteMapping;
import
org.springframework.web.bind.annotation.GetMapping;
import
org.springframework.web.bind.annotation.PathVariable;
import
org.springframework.web.bind.annotation.PostMapping;
import
org.springframework.web.bind.annotation.PutMapping;
import
org.springframework.web.bind.annotation.RequestBody;
import
org.springframework.web.bind.annotation.RestController;

@RestController

Product p2 = new
Product();
p2.setId("2");
p2.setName("Milk");
}
Name: Akshay Roll no:

public class ProductController {

private static Map<String, Product> pcontent = new HashMap<>();

static {
Product p1 = new Product();
p1.setId("1");
p1.setName("Bread");
pcontent.put(p1.getId(), p1);
Name: Akshay Roll no:

@GetMapping("/products")
public ResponseEntity<Object> getProduct() {
return new ResponseEntity<>(pcontent.values(),
HttpStatus.OK);
}

@PostMapping("/products")
public ResponseEntity<Object> addProduct(@RequestBody Product
product)
{
pcontent.put(product.getId(), product);
return new ResponseEntity<>("Product Added Successfully",
HttpStatus.CREATED);
}

@PutMapping("/products/{id}")
public ResponseEntity<Object> updateProduct(@PathVariable("id")
String id, @RequestBody Product product) {
if (!pcontent.containsKey(id)) {
return new ResponseEntity<>("Product not found",
HttpStatus.NOT_FOUND);
}
product.setId(id);
Product p2 = new
pcontent.put(id,
Product();
product);
p2.setId("2");
return new ResponseEntity<>("Product Updated Successfully",
p2.setName("Milk");
HttpStatus.OK);
}
}

@DeleteMapping("/products/{id}")
Name: Akshay Roll no:

public ResponseEntity<Object> deleteProduct(@PathVariable("id")


String id) { if (!pcontent.containsKey(id)) {
return new ResponseEntity<>("Product not found",
HttpStatus.NOT_FOUND);
}
pcontent.remove(id);
return new ResponseEntity<>("Product Deleted Successfully",
HttpStatus.OK);
}
}

ProductExampleApplication
package com.Product.ProductExample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProductExampleApplication {

public static void main(String[] args) {


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

}
Name: Akshay Roll no:

Output:

GET method
Name: Akshay Roll no:

POST Method

PUT Method
Name: Akshay Roll no:

DELETE Method

You might also like