Advance Java Practical Journal
ISBN 9788119221226

Highlights

Notes

  

Chapter 10: Practical based on SPRING boot and RESTFUL Web Services

Practical 10.1

Write a program to create a simple Spring Boot application that prints a message.

SpringBootDemoApplication.java package com.example.demo;

import org.springframework.boot.SpringApplication;

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

import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.bind.annotation.RequestMapping; @RestController

@EnableAutoConfiguration @SpringBootApplication

public class SpringBootDemoApplication {

public static void main(String[] args) {SpringApplication.run(SpringBootDemoApplication.class, args);

}

@RequestMapping(“/”)

public String home()

{

return “Hello World Spring boot”;

}

}

Output

Practical 10.2

Write a program to demonstrate RESTful Web Services with spring boot.

Product.java

package com.example.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 com.example.demo;

import java.util.HashMap;

import java.util.Map;

import org.springframework.http.HttpStatus;

import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;

@RestController

public class ProductServiceController {

private static Map<String, Product> productRepo = new

HashMap<>();

static {

Product honey = new Product(); honey.setId(“1”);

honey.setName(“Honey”);

productRepo.put(honey.getId(), honey);

Product almond = new Product(); almond.setId(“2”); almond.setName(“Almond”); productRepo.put(almond.getId(), almond);

}

@RequestMapping(value = “/products”)

public ResponseEntity<Object> getProduct() {

return new ResponseEntity<>(productRepo.values(), HttpStatus.OK);

}

@RequestMapping(value = “/products/{id}”, method = RequestMethod.PUT)

public ResponseEntity<Object> updateProduct(@PathVariable(“id”) String id, @RequestBody Product product) {

productRepo.remove(id); product.setId(id); productRepo.put(id, product);

return new ResponseEntity<>(“Product is updated successsfully”, HttpStatus.OK);

}

@RequestMapping(value = “/products/{id}”, method = RequestMethod.DELETE)

public ResponseEntity<Object> delete(@PathVariable(“id”) String id) {

productRepo.remove(id);

return new ResponseEntity<>(“Product is deleted successsfully”, HttpStatus.OK);

}

@RequestMapping(value = “/products”, method = RequestMethod.POST)

public ResponseEntity<Object> createProduct(@RequestBody Product product) {

productRepo.put(product.getId(), product);

return new ResponseEntity<>(“Product is created successfully”, HttpStatus.CREATED);

}

}

SpringBootRestfulServiceDemoApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;

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

public class SpringBootRestfulServiceDemoApplication {public static void main(String[] args) {

SpringApplication.run(SpringBootRestfulServiceDemoApplication.class, args);

}

}

Output

POST API URL is: http:mocalho,st.:8080/products

PUT API URL is: htt1p:llil,ocalhos:t: B0 80/products/3

DELETE API URL is: http://localhost:8080/products/3