user7688882
user7688882

Reputation:

Spring Boot RESTful application error

I working on a simple Spring Boot RESTful application every thing works fine except for listing all the customers (retrieving all of them from Mongodb). With my current code i should be able retrive all the customers.

Each time i type to my browser http://localhost:8080/customers i get error.

From my Java class 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>")
                    }
                });
            }

A part of my class 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;
    }}

I get this error:

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"

Upvotes: 1

Views: 290

Answers (1)

Adrian
Adrian

Reputation: 104

Your code is slightly incorrect. Update the following line:

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

to the following:

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

There is a difference between the url http://localhost:8080/customers and http://localhost:8080/customers/ in the ReST endpoints.

Upvotes: 1

Related Questions