Friday 15 May 2015

How do you load or even autowire a .json file from the classpath using annotations in java? -



How do you load or even autowire a .json file from the classpath using annotations in java? -

how load or autowire .json file classpath using spring annotations in java?

//something in controller in sts: @value("file:/resources/json/myjsonfile.json") resource jsontemplatefile;

create , abstract class public abstract class resourceloadingtest { @rule public testname testname = new testname(); protected string loadresource(string filename) throws ioexception { final url resource = getclass().getresource(filename); if (resource == null) { throw new illegalargumentexception("no resource file named <" + filename + "> loaded classpath."); } homecoming resources.tostring(resource, charsets.utf_8); } /** * loads json resource name derived running * test. derived reosurce name "[test-method-name].json". * example, if method called test method named * <code>testsomebehavior</code>, resource name loaded * <code>testsomebehavior.json</code> */ protected string loadcurrenttestjson() throws ioexception { homecoming loadresource(testname.getmethodname() + ".json"); } /** * loads xml resource name derived running * test. derived reosurce name "[test-method-name].xml". * example, if method called test method named * <code>testsomebehavior</code>, resource name loaded * <code>testsomebehavior.xml</code> */ protected string loadcurrenttestxml() throws ioexception { homecoming loadresource(testname.getmethodname() + ".xml"); } } --------------------------------------------------------------------- , in sub class public static void main(string[] args){ loadsetupmessage("myjsonfile.json"); } protected void loadsetupmessage(string filename) throws ioexception{ string rawmessage = this.loadresource(filename); system.out.println(string.format("loaded class %s", rawmessage); }

and place myjsonfile.json in same bundle hierarchy.

java json spring resources classpath

No comments:

Post a Comment