SpringBoot All Programs
SpringBoot All Programs
============================
1)
Displaying a message "Hi to Spring Boot" for the URL "/hi"
==========================================================
pom.xml:
========
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ofss</groupId>
<artifactId>CustomerAPI</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Mahindra comviva Customer API</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<java-version>1.8</java-version>
</properties>
</project>
CustomerAPIApp.java
====================
package com.ofss;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CustomerAPIApp {
}
src/main/application.properties:
================================
server.port = 8088
HiController.java:
==================
package com.ofss;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HiController {
@RequestMapping(method=RequestMethod.GET, value="/hi")
public String m1()
{
return "Hi to Spring Boot";
}
}
Steps to run:
=============
http://localhost:8088/hi
package com.ofss.customer;
CustomerController.java:
=========================
package com.ofss.customer;
import java.util.Arrays;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
@RestController
public class CustomerController {
@RequestMapping("/customers")
public List<Customer> getAllCustomers()
{
Customer c1=new Customer("Guru", "Murthy", 9731801675L,
"java.guru@yahoo.com");
Customer c2=new Customer("John", "Britto", 8351801675L,
"john.britto@yahoo.com");
Customer c3=new Customer("Odessa", "Lisburg", 7343480165L,
"odessa.lisburg@oracle.com");
Customer c4=new Customer("Nanditha", "Kumar", 9731829295L,
"knanditha@yahoo.com");
Customer c5=new Customer("Nital", "Shah", 9731801675L,
"nital.shah@gmail.com");
List<Customer> allCustomers=Arrays.asList(c1,c2,c3,c4,c5);
return allCustomers;
CustomerService.java:
=====================
package com.ofss.services;
import java.util.Arrays;
import java.util.ArrayList;
import org.springframework.stereotype.Service;
import com.ofss.customer.Customer;
@Service
public class CustomerService {
public CustomerService()
{
allCustomers.add(c1);
allCustomers.add(c2);
allCustomers.add(c3);
allCustomers.add(c4);
allCustomers.add(c5);
}
CustomerController.java:
========================
package com.ofss.customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ofss.services.CustomerService;
import java.util.*;
@RestController
public class CustomerController {
@Autowired
CustomerService customerService;
@RequestMapping("/customers")
public List<Customer> getAllCustomers()
{
return customerService.getAllCustomers();
}
}
@RequestMapping(value="/customers", method=RequestMethod.POST)
public void addCustomer(@RequestBody Customer cust)
{
customerService.addCustomer(cust);
}
In POSTMAN,
"firstName": "Raju",
"lastName": "Rama",
"phoneNumber": 123456243,
"emailId": "raju.rama@yahoo.com"
@RequestMapping(value="/customers/{name}", method=RequestMethod.PUT)
public void updateCustomer(@RequestBody Customer cust, @PathVariable("name")
String customerName)
{
System.out.println("Update method controller");
System.out.println("customer name is "+customerName);
customerService.updateCustomer(customerName, cust);
}
if ((allCustomers.get(i)).getFirstName().equals(customerName))
{
System.out.println("Matching....");
allCustomers.set(i, cust);
return;
}
In Postman:
URL: http://localhost:8088/customers/Guru
METHOD: PUT
{"firstName":"Guru","lastName":"Murthy
Sir","phoneNumber":9731801675,"emailId":"java.guru@yahoo.com"}
Headers:
Content-Type is application/json
Verify:
URL: http://localhost:8088/customers
METHOD: GET
@RequestMapping(value="/customers/{name}", method=RequestMethod.DELETE)
public void deleteCustomer(@PathVariable("name") String customerName)
{
System.out.println("DELETE method controller");
System.out.println("customer name is "+customerName);
customerService.deleteCustomer(customerName);
}
if ((allCustomers.get(i)).getFirstName().equals(customerName))
{
System.out.println("Deleting....");
allCustomers.remove(i);
return;
}
In Postman:
URL: http://localhost:8088/customers/Guru
METHOD: DELETE
Verify:
URL: http://localhost:8088/customers
METHOD: GET
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
CustomerAPIDataApplication.java:
================================
package com.ofss;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
SpringApplication.run(CustomerApiDataApplication.class, args);
Customer.java:
==============
package com.ofss.customer;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Customer {
@Id
String firstName;
String lastName;
long phoneNumber;
String emailId;
public Customer()
{
CustomerRepository.java:
========================
package com.ofss.services;
import org.springframework.data.repository.CrudRepository;
import com.ofss.customer.Customer;
CustomerService.java:
=====================
package com.ofss.services;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ofss.customer.Customer;
@Service
public class CustomerService {
@Autowired
CustomerRepository customerRepository;
}
}
From pom.xml:
==============
Remove the derby related dependency entry.
Add the following dependency entry for oracle
<dependency>
<groupId>com.oracle</groupId>
<artifactId>oracle</artifactId>
<version>11.2.0.2.0</version>
</dependency>
The last parameter for generating a POM will save you from pom.xml warnings
application.properties:
=======================
server.port=8091
#oracle db settings
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=system
spring.datasource.password=Password123456
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
EmployeeService.java:
=====================
package com.ofss.service;
import com.ofss.model.Employee;
import java.util.*;
EmployeeServiceImpl.java:
==========================
package com.ofss.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ofss.dao.EmployeeDaoImpl;
import com.ofss.model.Employee;
import com.ofss.service.EmployeeService;
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
EmployeeDaoImpl employeeDao;
@Override
public void insertEmployee(Employee emp) {
employeeDao.insertEmployee(emp);
@Override
public void insertEmployees(List<Employee> employees) {
}
@Override
public void getAllEmployees() {
@Override
public void getEmployeeById(String empid) {
EmployeeDao.java:
=================
package com.ofss.dao;
import java.util.List;
import com.ofss.model.Employee;
EmployeeDaoImpl.java:
======================
package com.ofss.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;
import com.ofss.model.Employee;
@Repository
public class EmployeeDaoImpl extends JdbcDaoSupport implements EmployeeDao{
@Autowired
DataSource dataSource;
@PostConstruct
private void initialize(){
setDataSource(dataSource);
}
@Override
public void insertEmployee(Employee emp) {
String sql = "INSERT INTO employee " +
"(empId, empName) VALUES (?, ?)" ;
getJdbcTemplate().update(sql, new Object[]{
emp.getEmpId(), emp.getEmpName()
});
System.out.println("Done inserting");
}
@Override
public void insertEmployees(final List<Employee> employees) {
System.out.println("insert employees method size "+employees.size());
String sql = "INSERT INTO employee " + "(empId, empName) VALUES
(?, ?)";
getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws
SQLException {
System.out.println("Cursor i is "+i);
Employee employee = employees.get(i);
ps.setString(1, employee.getEmpId());
ps.setString(2, employee.getEmpName());
}
}
@Override
public List<Employee> getAllEmployees(){
String sql = "SELECT * FROM employee";
List<Map<String, Object>> rows = getJdbcTemplate().queryForList(sql);
return result;
}
@Override
public Employee getEmployeeById(String empId) {
String sql = "SELECT * FROM employee WHERE empId = ?";
return (Employee)getJdbcTemplate().queryForObject(sql, new Object[]
{empId}, new RowMapper<Employee>(){
@Override
public Employee mapRow(ResultSet rs, int rwNumber) throws
SQLException {
Employee emp = new Employee();
emp.setEmpId(rs.getString("empId"));
emp.setEmpName(rs.getString("empName"));
return emp;
}
});
}
}
Main application:
SpringJdbcApplication.java
==========================
package com.ofss;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import com.ofss.model.Employee;
import com.ofss.service.EmployeeService;
@SpringBootApplication
public class SpringJdbcApplication {
@Autowired
EmployeeService employeeService;
List<Employee> employees=Arrays.asList(e2,e3,e4);
employeeService.insertEmployees(employees);
System.out.println(employeeService.getAllEmployees());
}
}