Friday, April 15, 2022

Feign Client Using Spring Boot | @EnableFeignClient | @FeignClient | FeignClient Spring Cloud



This is the video where I learnt Feign client, short and simple explanation.

1. Add dependency 

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
Add spring-cloud-starter-feign dependency to pom.xml 
2. Enable Feign Client 
@SpringBootApplicatispring-cloud-starter-feignon
@EnableFeignClients
Add Feign Client enable annotaton @EnableFeignClient in main class
3.Configer client url 
@FeignClient(value = "feignDemo",url = "http://localhost:8084/user/")
public interface FeignUtilService {
@GetMapping("name")
public String getName();

@GetMapping("city")
public String getCity();
}
All set to go....
This is the most simples way. But there are some other best practicess will update on the go. 
up to now this is what I know. :D 
Happy Coding guys cheers !!!

Monday, April 11, 2022

Create MySql connection with Spring Boot and JPA

 create a project with spring Initializer :



<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

If you add JPA and MySql int to your project, you can simply add spring JPA and MySQL driver dependencies into pom.xml 


The first thing is to create an application property with MySQL :

spring.datasource.url=jdbc:mysql://localhost:3306/sms?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.datasource.username=root
spring.datasource.password=tstc123

#Hibernate
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

#Hibernate auto ddl
spring.jpa.hibernate.ddl-auto=update

logging.level.org.hibernate.SQL=DEBUG 

Wednesday, April 6, 2022

Whitelabel Error Page

When I start my first spring boot application, I was very happy about after first Hello World program

just one hello method and return Hello world 


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorld {

@GetMapping(path = "/hello-world")
public String Hello(){
return "Hello World !!!";
}
}
and i run with this with 
http://localhost:8080/hello-world
Guess what !!!
got a big fat error page 

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Apr 07 07:17:58 AEST 2022
There was an unexpected error (type=Not Found, status=404).
No message available

Solution for this funny message 

This is clearly not an error with my code. code or syntax are all correct.
the problem lies on my folder stucture

com
   +- APP
         +- Application.java  <--- your main class should be here, above your controller classes
         |
         +- model
         |   +- user.java
         +- controller
             +- UserController.java
my conroller package was out side the main package. 
more technical perspective 
it should be 

Feign Client Using Spring Boot | @EnableFeignClient | @FeignClient | FeignClient Spring Cloud

This is the video where I learnt Feign client, short and simple explanation. 1. Add dependency  <dependency> <groupId> ...