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 Exploring Your API With New Tools - Postman

Kaho Kang
Kaho Kang
12,383 Points

Why my API is not saving my post after I re-started the server?

The only JSON objects that remains are the dummy data that I wrote in the DataLoader class under the override run method. When I use Postman to post some new data, it shows........But when I re-started the server, they all gone. I thought JPA will automatically save that for me.... But seems I have to write the save method myself? In order to save my post to h2 database?

If so, what's the solution? Craig Dennis

1 Answer

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

I think this is because by default you have testing in-memory database that is get erased when you finish the app.

If you want to set up a database with Spring-Data-Rest, add the following in the application.properties file

# embedded database for example, instead of testing
# can be server database if you want, see giflib-hibernate from Chris
spring.datasource.url = jdbc:h2:./data/home
spring.datasource.password = sa
spring.datasource.username = sa
spring.datasource.driver-class-name= org.h2.Driver
# change create-drop to update for example, and then DataLoader will update things all the time
spring.datasource.schema = create-drop

See my application.properties for project-10 Spring-Data-Rest project for working example.

For more about default Spring application properties see here