gpt4 book ai didi

java - Spring Boot RESTful 应用程序错误

转载 作者:行者123 更新时间:2023-11-30 06:50:35 26 4
gpt4 key购买 nike

我正在开发一个简单的 Spring Boot RESTful 应用程序,除了列出所有客户(从 Mongodb 检索所有客户)之外,一切正常。使用我当前的代码,我应该能够检索所有客户。

每次我在浏览器中输入http://localhost:8080/customers我都会收到错误。

来 self 的 Java 类 CustomerRestController:

@CrossOrigin
@GetMapping("/customers")
public ArrayList<Customer> getCustomers()
{
customerDAO = new CustomerDAO();
return customerDAO.getCustomers();
}

enter image description here

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"

最佳答案

您的代码有点不正确。更新以下行:

$.getJSON("http://localhost:8080/customers/",  function(data)

以下内容:

$.getJSON("http://localhost:8080/customers",  function(data)

网址 http://localhost:8080/customers 之间存在差异和 http://localhost:8080/customers/在 ReST 端点中。

关于java - Spring Boot RESTful 应用程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42868045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com