Commit 4181f611 authored by Narendar Vakiti's avatar Narendar Vakiti

added loggers

parent 8e367367
......@@ -29,6 +29,19 @@
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-log4j -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
......
......@@ -2,6 +2,21 @@ package com.bookstore.bean;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
/**
* This bean for hold playload information of book and author details
* @author nvakiti
*
*/
@Getter @Setter
@ToString @AllArgsConstructor @NoArgsConstructor
public class BookDetails implements Serializable{
/**
......@@ -17,83 +32,5 @@ public class BookDetails implements Serializable{
private String authorName;
private String address;
public BookDetails() {
}
public BookDetails(int bookId, String bookName, double bookPrice, boolean active, int authorId, String authorName,
String address) {
super();
this.bookId = bookId;
this.bookName = bookName;
this.bookPrice = bookPrice;
this.active = active;
this.authorId = authorId;
this.authorName = authorName;
this.address = address;
}
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public double getBookPrice() {
return bookPrice;
}
public void setBookPrice(double bookPrice) {
this.bookPrice = bookPrice;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public int getAuthorId() {
return authorId;
}
public void setAuthorId(int authorId) {
this.authorId = authorId;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "[bookId=" + bookId + ", bookName=" + bookName + ", bookPrice=" + bookPrice + ", active="
+ active + ", authorId=" + authorId + ", authorName=" + authorName + ", address=" + address + "]";
}
}
......@@ -2,6 +2,8 @@ package com.bookstore.controller;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
......@@ -17,17 +19,29 @@ import com.bookstore.bean.BookDetails;
import com.google.gson.Gson;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
/**
* This class for add book details, get the book information and author details
* @author nvakiti
*
*/
@RestController
public class BookStoreController {
private static final Logger logger = LoggerFactory.getLogger(BookStoreController.class);
/**
* Calling Books Rest API to fetch books details
* @author nvakiti
* @return json
*/
@GetMapping("/getbooks")
@HystrixCommand(fallbackMethod = "getBooksFallback")
public String getBooks() {
logger.info("Started getBooks()");
String uri = "http://localhost:8083/getbookdetails";
logger.info("API URI "+uri);
String response = null;
try {
RestTemplate restTemplate = new RestTemplate();
......@@ -36,31 +50,37 @@ public class BookStoreController {
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
response = result.getBody();
logger.info("Book Details Respose "+response);
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
}
logger.info("End getBooks()");
return response;
}
/**
* Fall back method, when /getbooks service is down then fall back method will call to show info
* @author nvakiti
* @return
*/
@SuppressWarnings("unused")
private String getBooksFallback() {
logger.info("Started fallback getBooksFallback() for /getbooks");
return "Service is not available, please try again later";
}
/**
* Calling Author Rest API to fetch author details
* @author nvakiti
* @return
*/
@GetMapping("/getauthor")
public String getAuthor() {
logger.info("Started getAuthor()");
String uri = "http://localhost:8083/getauthordetails";
logger.info("API URI "+uri);
String response = null;
try {
RestTemplate restTemplate = new RestTemplate();
......@@ -69,19 +89,23 @@ public class BookStoreController {
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
response = result.getBody();
logger.info("Author Details Respose "+response);
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
}
logger.info("End getAuthor()");
return response;
}
@PostMapping("/addbooks")
public String addBooks(@RequestBody BookDetails bookDetails) {
logger.info("Started addBooks()");
String uri = "http://localhost:8083/addbookdetails";
logger.info("API URI "+uri);
String response = null;
try {
String request = new Gson().toJson(bookDetails);
logger.info("Book Details Payload "+request);
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
......@@ -89,9 +113,11 @@ public class BookStoreController {
HttpEntity<String> entity = new HttpEntity<String>(request, headers);
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.POST, entity, String.class);
response = result.getBody();
logger.info("Add Books Respose "+response);
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
}
logger.info("End addBooks()");
return response;
}
......
server.port=8082
spring.application.name=bookstore
logging.level.root=INFO
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment