C06 Assignment
Spring Boot and RESTful Web Services
Q1. Write a program to create a simple spring boot application that prints a
message
Step 1: go to start.sping.io
Step 2: after selecting all the options properly as required and selecting required dependencies
Generate project.
Step 3: Import the generated project in eclipse.
MessageController.java
package mca.practical.Demo;
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 World!");
return "message";
}
}
Message.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 th:text="${message}"></h1>
</body>
</html>
Q.2 Write a program to demonstrate RESTFUL web services with spring boot.
Code:
Product.java
package mca.practical.Demo;
public class Product {
private String id; private
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;
}
ProductServiceController.java
package mca.practical.Demo; import
java.util.*; import
org.springframework.http.*; import
org.springframework.web.bind.annotation.*;
@RestController // type public class ProductServiceController { private static
Map<String, Product> pcontent = new HashMap<>();
static {
Product honey = new Product();
honey.setId("1"); honey.setName("Honey");
pcontent.put(honey.getId(), honey);
// By default it gets the value so we don't need to give. @RequestMapping(value =
"/products") // blue is url products public ResponseEntity<Object> getProduct() { return new
ResponseEntity<Object>(pcontent.values(), HttpStatus.OK);
// We are getting data using Post method.
@RequestMapping(value = "/products", method = RequestMethod.POST) public
ResponseEntity<Object> addProduct(@RequestBody Product product) {
pcontent.put(product.getId(), product);
return new ResponseEntity<Object>("Product Added Succesfully", HttpStatus.OK);
// We are using Put method.
@RequestMapping(value = "/products/{id}", method = RequestMethod.PUT)
public ResponseEntity<Object> updateProduct(@PathVariable("id") String id,
@RequestBody Product product) {
pcontent.remove(id); product.setId(id);
pcontent.put(product.getId(), product);
return new ResponseEntity<Object>("Product is Updated Successfully", HttpStatus.OK);
// We are deleting using delete method.
@RequestMapping(value = "/products/{id}", method = RequestMethod.DELETE) public
ResponseEntity<Object> updateProduct(@PathVariable("id") String id) {
pcontent.remove(id);
return new ResponseEntity<Object>("Product is deleted Successfully", HttpStatus.OK);
DemoApplication.java
package mca.practical.Demo;
import org.springframework.boot.SpringApplication; import
org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
Postman:
Get the Link