Akshay
Akshay
Sreedhar 03
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:
</body>
</html>
FirstSpringApplication.java
package com.FirstSpringApp.FirstSpringApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FirstSpringApplication {
}
Name: Akshay Roll no:
Sreedhar 03
Output:
http://localhost:8080/message
Name: Akshay Roll no:
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:
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:
ProductExampleApplication
package com.Product.ProductExample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProductExampleApplication {
}
Name: Akshay Roll no:
Output:
GET method
Name: Akshay Roll no:
POST Method
PUT Method
Name: Akshay Roll no:
DELETE Method