I am avery beginner at SPring MVC, Mongodb and java. I am trying to make a sample application so that the user can serach, create, update and delete a customer:
My CustomerDAO class:
package com.example;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
import static org.glassfish.hk2.utilities.reflection.Pretty.collection;
import static org.springframework.core.convert.TypeDescriptor.collection;
import org.springframework.stereotype.Component;
@Component
public class CustomerDAO
{
private ArrayList<Customer> customers;
public CustomerDAO()
{
customers = new ArrayList();
}
public ArrayList<Customer> getCustomers()
{
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("testdb");
MongoCollection<Document> col = database.getCollection("customer");
MongoCursor<Document> cur = col.find().iterator();
while(cur.hasNext())
{
Document doc = cur.next();
List list = new ArrayList(doc.values());
customers.add(new Customer((int) Float.parseFloat(list.get(1).toString()), list.get(2).toString(), list.get(3).toString()));
}
mongoClient.close();
return customers;
}
public ArrayList<Customer> get(int id)
{
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("testdb");
MongoCollection<Document> col = database.getCollection("customer");
BasicDBObject queary = new BasicDBObject("id", new BasicDBObject("$eq", id));
MongoCursor<Document> cur = col.find(queary).iterator();
while(cur.hasNext())
{
Document doc = cur.next();
List list = new ArrayList(doc.values());
customers.add(new Customer((int) Float.parseFloat(list.get(1).toString()), list.get(2).toString(), list.get(3).toString()));
}
mongoClient.close();
return customers;
}
public ArrayList<Customer> delete(Long id)
{
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("testdb");
MongoCollection<Document> col = database.getCollection("customer");
BasicDBObject queary = new BasicDBObject("id", new BasicDBObject("$eq", id));
col.deleteOne(eq("id", id));
mongoClient.close();
return customers;
}
public ArrayList<Customer> update(Long id)
{
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("testdb");
MongoCollection<Document> col = database.getCollection("customer");
BasicDBObject queary = new BasicDBObject("id", new BasicDBObject("$eq", id));
MongoCursor<Document> cur = col.find(queary).iterator();
while(cur.hasNext())
{
Document doc = cur.next();
List list = new ArrayList(doc.values());
customers.add(new Customer((int) Float.parseFloat(list.get(1).toString()), list.get(2).toString(), list.get(3).toString()));
}
mongoClient.close();
return customers;
}
}
My CustomerRestController class:
package com.example;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static org.springframework.web.bind.annotation.RequestMethod.DELETE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
import static org.springframework.web.bind.annotation.RequestMethod.PUT;
@RestController
public class CustomerRestController
{
@Autowired
private CustomerDAO customerDAO;
@CrossOrigin
@GetMapping("/customers")
public ArrayList<Customer> getCustomers()
{
customerDAO = new CustomerDAO();
return customerDAO.getCustomers();
}
@CrossOrigin
@GetMapping("/customers/{id}")
public ArrayList<Customer> getCustomer(@PathVariable("id") int id)
{
customerDAO = new CustomerDAO();
return customerDAO.get(id);
}
@CrossOrigin
@PostMapping(value = "/customers")
public ArrayList<Customer> createCustomer(@RequestBody Customer customer) {
customerDAO = new CustomerDAO();
return customerDAO.create(customer);
}
@CrossOrigin
@DeleteMapping("/customers/{id}")
public ArrayList<Customer> deleteCustomer(@PathVariable Long id) {
customerDAO = new CustomerDAO();
return customerDAO.delete(id);
}
@CrossOrigin
@PutMapping("/customers/{id}")
public ArrayList<Customer> updateCustomer(@PathVariable Long id, @RequestBody Customer customer) {
customerDAO = new CustomerDAO();
return customerDAO.update(id, customer);
}
}
My restclient page:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<input name="search" type="text" maxlength="100" id="search"/>
<button onclick="searchID()"> Search ID </button>
<button onclick="saveID()"> Save ID </button>
<button onclick="deleteID()"> Delete ID </button>
<button onclick="updateID()"> Update ID </button>
<button onclick="showAll()"> Show All </button>
<div id="persons"></div>
<script>
function searchID()
{
var id = document.getElementById("search").value;
$("#persons").html("");
$.getJSON("http://localhost:8080/customers/" + id, function(data)
{
for (var i in data) {
$('#persons').append("<p>ID: " + data[i].id + "</p>")
$('#persons').append("<p>First name: " + data[i].firstName + "</p>")
$('#persons').append("<p>Last name: " + data[i].lastName + "</p><br>")
}
});
}
function showAll()
{
$("#persons").html("");
$.getJSON("http://localhost:8080/customers/", function(data)
{
for (var i in data) {
$('#persons').append("<p>ID: " + data[i].id + "</p>")
$('#persons').append("<p>First name: " + data[i].firstName + "</p>")
$('#persons').append("<p>Last name: " + data[i].lastName + "</p><br>")
}
});
}
</script>
</body>
</html>