Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
B
bookstore-app
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Narendar Vakiti
bookstore-app
Commits
4181f611
Commit
4181f611
authored
Mar 10, 2020
by
Narendar Vakiti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added loggers
parent
8e367367
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
61 additions
and
84 deletions
+61
-84
pom.xml
pom.xml
+13
-0
BookDetails.java
src/main/java/com/bookstore/bean/BookDetails.java
+15
-78
BookStoreController.java
...in/java/com/bookstore/controller/BookStoreController.java
+32
-6
application.properties
src/main/resources/application.properties
+1
-0
No files found.
pom.xml
View file @
4181f611
...
...
@@ -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>
...
...
src/main/java/com/bookstore/bean/BookDetails.java
View file @
4181f611
...
...
@@ -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
+
"]"
;
}
}
src/main/java/com/bookstore/controller/BookStoreController.java
View file @
4181f611
...
...
@@ -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
;
}
...
...
src/main/resources/application.properties
View file @
4181f611
server.port
=
8082
spring.application.name
=
bookstore
logging.level.root
=
INFO
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment