Grails 3 - access a resource
I need to get the path to a static resource residing in a assets/schemas/resource.json
Grails 3 service.
At the moment it is defined as
private final String SCHEMA = 'grails-app/assets/schemas/resource.json',
which is great for the environment development
, but certainly not for production (since it would be located in <app_root>/assets/resource.json
.
I tried to find how to use the Asset Pipeline in my case, but so far I really have no idea: P
Thanks in advance!
source to share
This is described in the docs. http://bertramdev.github.io/grails-asset-pipeline/guide/usage.html
In a controller or service, enter assetResourceLocator
and useassetResourceLocator.findAssetForURI()
source to share
It works locally, but not when deployed to the server. Using Grails 3.1.0, Java 1.8.0_91 and Tomcat 8.0.33.
assetResourceLocator?.findAssetForURI('myFolder/placeholder.jpg')?.byteArray
returns
groovy.lang.MissingPropertyException: No such property: byteArray for class: org.springframework.web.context.support.ServletContextResource
EDIT: Solved:
assetResourceLocator?.findAssetForURI('myFolder/placeholder.jpg')?.getInputStream()?.bytes
source to share
Complete example:
class ExampleService {
def assetResourceLocator
def someMethod() {
Resource res = assetResourceLocator.findAssetForURI('test.css')
String url = res.getURL()
String uri = res.getURI()
}
}
Source: http://bertramdev.github.io/grails-asset-pipeline/guide/usage.html
source to share