RESTful web service producer and Consumer

Producer of RESTful web service
- Produces service as data in a structured format
- The structured format can be XML, JSon , CSV, Excel or any
- The producer provides an end point as Application Programming Interface (API)
- The API must be available over internet protocol HTTP or HTTPS
- The data available from RESTful web service is called as payload.
Refer to the following video to understand the process of producer Spring boot App.
Payload or output of the above video tutorial in Postman

How to Consume restful web service in spring boot application
1. Create a Spring boot Application as the consumer app. Use the following Links to know creation process a spring boot application in Eclipse or STS.
- Creating Spring boot Application in Eclipse . Click here
- Creating Spring boot Application in STS. Click here
2. Add Tomcat embed jasper and JSTL dependency in pom.xml
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.cyber.deb.consumer</groupId> <artifactId>RestConsumer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>RestConsumer</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3. Create a class to represent JSon name–value pairs.
Patient.java
package com.cyber.deb.boot.models; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties public class Patient { int age; String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
4. Create a Controller to get data by using RestTemplate
ConsumerController.java
package com.cyber.deb.boot.controllers; import java.util.Arrays; import java.util.List; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.client.RestTemplate; import org.springframework.web.servlet.ModelAndView; import com.cyber.deb.boot.models.Patient; @Controller public class ConsumerController { RestTemplate restTemplate=new RestTemplate(); @RequestMapping("/show") public ModelAndView show() { ModelAndView mv=new ModelAndView(); mv.setViewName("showres"); ResponseEntity<Patient[]> responseEntity = restTemplate.getForEntity("http://localhost:8080/showPersons", Patient[].class); Patient[] responseBody = responseEntity.getBody() List<Patient> allPatient = Arrays.asList(responseBody); mv.addObject("myjson", allPatient); return mv; } }
5. Edit application.properties for changing default port and to specify jsp as the view
application.properties
spring.mvc.view.prefix:/WEB-INF/jsp/ spring.mvc.view.suffix:.jsp server.port=8081
6. Add JSP file into WEB-INF/jsp folder in webapp.
showres.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <div align="center"> <h1>WS Client</h1> <table border="1"> <tr><th>Name</th><th>Age</th></tr> <c:forEach items="${myjson}" var="mj"> <tr> <td>${mj.name}</td><td> ${mj.age} </td> </tr> </c:forEach> </table> </div> </body> </html>
Run the producer app in port 8080
Run the consumer app to view following response in web browser
