I have a bunch of data that exists in a separate file and am trying to figure out how to automatically load this data into the db when the grails application starts up.
Asked
Active
Viewed 2,375 times
1 Answers
4
I put the json file into grails-app/conf/resources
and edited Bootstrap.groovy
.
At the top of the file, I injected the grails application:
class BootStrap {
def grailsApplication
...
And later on I loaded the data:
if (!Cars.count()) {
// add cars
def filePath = "resources/carsData.json"
def text = grailsApplication.getParentContext().getResource("classpath:$filePath").getInputStream().getText()
def json = JSON.parse(text)
for (carData in json) {
def c = new Cars(
name: carData ["name"],
year: carData ["year"]
).save(failOnError: true);
}
}

Charlotte Tan
- 2,452
- 2
- 20
- 24
-
I've been trying to do this via unmarshalling rather then this manual approach. Any ideas how to do that? – Richie Mackay Nov 20 '14 at 17:05