Step-1: Create a new Spring Boot project from start.spring.io as it is shown in below image. Make sure you have added MySQL deiver and Data JPA and Spring web dependency. Also choose War as Packaging. Click on Generate to create the project.
Step-2: extract the project created in previous step and import in your eclipse tool as shown in below image. (Project structure should be same as below)
Step-3: Make sure to enter the below mentioned entries to application.properties file user your MySQL user name and password in following places.
Step:-4: Lets start the application and it should be up and running as below
Step:-5 Lets create a demo project with correct structure which is followed in an organization level.
Create Product.java and use the below code
package com.ncteam.weblogic.bean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity(name="NC_PRODUCT")
public class Product {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int productID;
private String productName;
private String price;
public Product() {
}
public Product(int productID, String productName, String price) {
super();
this.productID = productID;
this.productName = productName;
this.price = price;
}
public int getProductID() {
return productID;
}
public void setProductID(int productID) {
this.productID = productID;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
Create ProductDAO.java and use below code:
package com.ncteam.weblogic.dao;
import org.springframework.data.repository.CrudRepository;
import com.ncteam.weblogic.bean.Product;
public interface ProductDAO extends CrudRepository<Product, Integer>{
}
Create ProductService and use below code:
package com.ncteam.weblogic.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ncteam.weblogic.bean.Product;
import com.ncteam.weblogic.dao.ProductDAO;
@Service
public class ProductService {
@Autowired
private ProductDAO productDAO;
public List<Product> getAllProduct(){
return (List<Product>) productDAO.findAll();
}
public Product saveAllProduct(Product product) {
// TODO Auto-generated method stub
return productDAO.save(product);
}
}
Create ProductController and use below code:
package com.ncteam.weblogic.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ncteam.weblogic.bean.Product;
import com.ncteam.weblogic.service.ProductService;
@RestController
public class ProductController {
@Autowired
private ProductService service;
@GetMapping("/products")
public List<Product> getAllProduct(){
return service.getAllProduct();
}
@PostMapping("/products")
public Product saveAllProduct(@RequestBody Product product){
return service.saveAllProduct(product);
}
}
That's it, Execute the project and test the endpoint of our API as shown in below image:
That's all for today - we would see how to use datasource and deploy the same project in weblogic server in our next blog.
No comments:
Post a Comment