2017-03-17 8 views
1

私は単純なSpring Boot RESTfulアプリケーションに取り組んでいますが、すべての顧客を一覧表示する以外はすべて正常に動作します(すべてMongodbから取得します)。私の現在のコードでは、すべての顧客を検索することができます。SpringブートRESTfulアプリケーションエラー

私のブラウザに入力するたびにhttp://localhost:8080/customers私はエラーになります。

@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の一部:私のJavaクラスCustomerRestControllerから

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

CustomerDAOクラスを共有できますか? – hya

+0

'list.get(1).toString()'は浮動小数点数ではなく文字列 'com.myproject.model.Customer'を返します。おそらく、 'List'インデックスが0から始まり、実際には' Float.parseFloat(list.get(0).toString()) 'を意味していなかったでしょうか? – Andreas

+0

私は索引付けの問題は考えていません。私はあなたの提案を試してみましたが、それらのうちの答えは以下の通りです:( –

答えて

1

コードが少し間違っています。次へ

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

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

RESTエンドポイントでのURL http://localhost:8080/customershttp://localhost:8080/customers/の間に違いがあり、以下の行を更新します。

+0

私はあなたの提案を試みました。残念ながら、それはエラーを修正しませんでした:) –

関連する問題