what to learn:
requestparam
how to use variables of app.props
transaction managemt
http error codes
# 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 :
# Changing server port
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
----------------------------------------------------------------------
@JoinColumn(name="") for foreign key column name,
@Tabel(name= "") for entity table name,
@Column(name=""), for changing normal column name
@Entity, for making table of classs,
@Id, for making primary key of table
@OneToOne(mappedBy= "", cascade= cascadeType.All), mappedby will use where foreign
key should not add, cascasde: when save parent data, child data should also add
auto.
// 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:
public interface UserRepository extends CrudRepository<User, Integer> {
public List<User> findByName(String name);
public List<User> findByNameAndCity (String name, String city);
public List<User> findByNameStartingWith (String name, String city);
public List<User> findByNameEndingWith (String name, String city);
public List<User> findByNameContaining (String name, String city);
public List<User> findByAgeLessThan (String name, String city);
}
-----------------------------------------------------------------
In repo interface, we need to write own query for methods then,
public interface UserRepository extends CrudRepository<User, Integer> {
// JPQL, java persistance query langauge
@Query("select u FROM User u")
public List<User> getAllUser();
// JPQL, java persistance query langauge
@Query("select u From User u WHERE u.name =:n and u.city=:c")|
public List<User> getUserByName(@Param("n") String name, @Param("c") String city);
// Native query, means simple SQL
@Query(value = "select * from user", nativeQuery =true)
public List<User> getUsers();
}
---------------------------------------
// 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::
spring cloud starter
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
--------------------------For API gateway::
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