Spring Boot and API
Spring Boot and API
requestparam
how to use variables of app.props
transaction managemt
# Change embedded server from tomcat to other: First exclude tomcat and then add
new server in pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
application.properties :
server.port=5000
# Hibernate configuration
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database=default
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5Dialect
# Database
spring.datasource.url= jdbc:mysql://localhost:3306/customer
spring.datasource.username=root
spring.datasource.password=rootroot
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
----------------------------------------------------------------------
// to stop looping in API in different mapping, when we fetch customer and his
bank, due to mapping, bank will again fetch customer due to foreignkey, so will
come in loop.
@JsonBackReference , in child class, but cannot be used with collections.
So for collection in child, we use, @JsonIgnore
@JsonManagedReference, in parent class
-----------------------------------------------
if want to write custom mthods in repo:
---------------------------------------
// for sending entity reponse from API:
@GetMapping("/books")
public ResponseEntity<List<Book>> getBooks () {
List<Book> list = bookService.getAllBooks();
if (list.size() <= 0) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
return Response Entity.of (Optional.of(list));
}
----------------------------------------------
@PostMapping("/books")
public ResponseEntity<Book> add Book (@RequestBody Book book) {
Book b = null;
try {
b = this.bookService.addBook (book);
System.out.println(book);
return ResponseEntity.status(HttpStatus.
CREATED).build();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return ResponseEntity.status (HttpStatus.
INTERNAL_SERVER_ERROR).build();
}
--------------
Eureka server::
@EnableEurekaClient
spring.application.name=USER-SERVICE
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
eureka.instance.prefer-ip-address=true
starter gateway
webflux
spring cloud
eureka
cloud:
gateway:
routes:
- id: USER-SERVICE
uri: lb://USER-SERVICE
predicates:
- Path=/user/**
-------------------------------------
For resillience4J circuit breaker,
AOP
Acutator
ResalliancJ4
@CiruitBreaker(name="ratingHotelBreaker", fallbackmethod="ratingHotelFallback")
// add in contoller where , Method which is calling mutiple apis
-------
rest template
web client
embeddedID
Transactional