Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Manual Testing

Jef Davis
Jef Davis
29,162 Points

500 Internal Error POST /courses

My code passes all the tests from previous videos, but I'm getting an error code 500 when I use Postman to post a course. My console in IntelliJ has the following information:

"C:\Program Files\Java\jdk1.8.0_25...[redacted for readability]...
[Thread-0] INFO org.eclipse.jetty.util.log - Logging initialized @1633ms
[Thread-0] INFO spark.webserver.JettySparkServer - == Spark has ignited ...
[Thread-0] INFO spark.webserver.JettySparkServer - >> Listening on 0.0.0.0:4567
[Thread-0] INFO org.eclipse.jetty.server.Server - jetty-9.3.2.v20150730
[Thread-0] INFO org.eclipse.jetty.server.ServerConnector - Started ServerConnector@7cfbdbb{HTTP/1.1,[http/1.1]}{0.0.0.0:4567}
[Thread-0] INFO org.eclipse.jetty.server.Server - Started @2312ms
[qtp15229841-18] ERROR spark.webserver.MatcherFilter - 
co.intellimatix.courses.exc.DaoException: Problem adding course.
    at co.intellimatix.courses.dao.Sql2oCourseDao.add(Sql2oCourseDao.java:28)
    at co.intellimatix.courses.Api.lambda$main$0(Api.java:26)
    at co.intellimatix.courses.Api$$Lambda$1/1973538135.handle(Unknown Source)
    at spark.ResponseTransformerRouteImpl$1.handle(ResponseTransformerRouteImpl.java:47)
    at spark.webserver.MatcherFilter.doFilter(MatcherFilter.java:162)
    at spark.webserver.JettyHandler.doHandle(JettyHandler.java:61)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:189)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:119)
    at org.eclipse.jetty.server.Server.handle(Server.java:517)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:302)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:242)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:245)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
    at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:75)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:213)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:147)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)
    at java.lang.Thread.run(Thread.java:745)

It looks as though there's a problem at the post method in my api.java file. Here's what I have there:

        Sql2o sql2o = new Sql2o("jdbc:h2:~/reviews.db; INIT=RUNSCRIPT from 'classpath:db.init.sql'", "", "");
        CourseDao courseDao = new Sql2oCourseDao(sql2o);
        Gson gson = new Gson();

        post("/courses", "application/json", (req, res) -> {
            Course course = gson.fromJson(req.body(), Course.class);
            courseDao.add(course);
            res.status(201);
            return course;
        }, gson::toJson);

The add method in Sql2oCourseDao.java is:

    private final Sql2o sql2o;

    public Sql2oCourseDao(Sql2o sql2o) {
        this.sql2o = sql2o;
    }

    @Override
    public void add(Course course) throws DaoException {
        String sql = "INSERT INTO courses(name, url) VALUES (:name, :url)";
        try (Connection con = sql2o.open()) {
            int id = (int) con.createQuery(sql).bind(course).executeUpdate().getKey();
            course.setId(id);
        } catch (Sql2oException ex) {
            throw new DaoException(ex, "Problem adding course.");
        }
    }

I'll be happy to provide other snips of my code, if it helps. Any insight you can provide is greatly appreciated!

6 Answers

Jef Davis
Jef Davis
29,162 Points

So, after playing around with Spark and Postman using some other tutorials I've found, I returned back to this workshop and I found my error:

Sql2o sql2o = new Sql2o("jdbc:h2:~/reviews.db; INIT=RUNSCRIPT from 'classpath:db.init.sql'", "", "");

my init.sql file should be 'db/init.sql' NOT 'db.init.sql'

Got it! My code works now.

Fahad Mutair
Fahad Mutair
10,359 Points

Oh nice work , it's great mistake to learn from

Jeremiah Shore
Jeremiah Shore
31,168 Points

BTW, you can use a language identifier in the markdown to style the java syntax specifically with fancy colors. Just include "java" right after the backticks, e.g... "```java" instead of just "```"

System.out.println("Hello World!"); //now THAT's fancy!
Fahad Mutair
Fahad Mutair
10,359 Points

hi Jefferson Davis , did you set Postman header to content-type and application/json

Jef Davis
Jef Davis
29,162 Points

Yes. I double-checked in the log to be sure. I'm getting an error at _sendRequest @ common.js:120111.

Jef Davis
Jef Davis
29,162 Points

I'm wondering if Craig Dennis has any thoughts on this one. I've been scouring the web for hours with no luck.

Jef Davis
Jef Davis
29,162 Points

I've posted the sections above that have to do with the post method to add a course already. Like I said, my code passes all the tests the workshop uses in the previous video...just no luck in getting beyond this error 500 at this point.

So, I've made a little get method to potentially narrow-down what might be wrong by following this tutorial https://sparktutorials.github.io/2015/04/03/spark-lombok-jackson-reduce-boilerplate.html

And it works!

I've dropped the following code from that tutorial into the api.java file of my treehouse project and I get a valid response through Postman.

        //Checking this thing...
        get("/posts", (req, res) -> {
        return "Hello Sparkingly World!";
        });

This little exercise lets me know it must be something in my code for the treehouse project.... do you see any issues in the code I've shared in my original post?

Fahad Mutair
Fahad Mutair
10,359 Points

I don't see any difference between your code and mine

Jef Davis
Jef Davis
29,162 Points

What's your github username?

Fahad Mutair
Fahad Mutair
10,359 Points

i downloaded my work on workspace heres snapshot to my work.

Jef Davis
Jef Davis
29,162 Points

Thank you very much for the link to your files, Fahad Mutair !

Everything that I've done so far seems to match yours exactly. I'm still going through it to try to find some small error that might be preventing Postman from creating a course.

I've looked into this as I had similar error. After debugging I saw that the exception I was getting was:

org.sql2o.Sql2oException: Could not acquire a connection from DataSource - No suitable driver found for jdbc:h2:~/reviews.db;INIT=RUNSCRIPT from 'classpath:db/init.sql'

This was because my H2 dependency was added as testImplementation in build.gradle, which is default option when you copy it from maven central. So the H2 wasn't being added when I was running the app locally. To fix this I had change testImplementation to implementation:

implementation group: 'com.h2database', name: 'h2', version: '2.1.214'