我正在开发一个简单的Spring Boot RESTful应用程序,除了列出所有客户(从Mongodb检索所有客户)之外,每件事情都可以正常工作。使用我当前的代码,我应该能够检索所有客户。
每次我输入浏览器http://localhost:8080/customers
时都会收到错误。
从我的Java类CustomerRestController:
@CrossOrigin
@GetMapping("/customers")
public ArrayList<Customer> getCustomers()
{
customerDAO = new CustomerDAO();
return customerDAO.getCustomers();
}
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>")
}
});
}
我的班级CustomerDAO的一部分:
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("customers");
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;
}}
我收到此错误:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Mar 17 23:48:40 EET 2017
There was an unexpected error (type=Internal Server Error, status=500).
For input string: "com.myproject.model.Customer"
答案 0 :(得分:1)
您的代码稍有不正确。更新以下行:
$.getJSON("http://localhost:8080/customers/", function(data)
以下内容:
$.getJSON("http://localhost:8080/customers", function(data)
ReST端点中的网址http://localhost:8080/customers和http://localhost:8080/customers/之间存在差异。