How do you load or even autowire a .json file from the classpath using annotations in java? -
How do you load or load a .json file from classpath using Spring Annotations in Java?
// Something like this in my controller in STS: @value ("file: /resources/json/myJsonfile.json") resource jsonTemplateFile;
create an abstract class:
public abstract Class Resourceloadingstst {@Royal Public TestName TestName = New Testname (); Protected strings loadersource (string filename) throws IOException {end URL resource = getClass (). GetResource (fileName); If (resource == empty) {new illegal employment exception ("no resource file is named;" + + + filename + "& gt; can be loaded from classpath."); } Returns Resources. Casting (Resources, Chart. UTF_8); } / ** * A JSON loads the resource whose name is currently being played * from the trial, the name of the derived renamed is "[test-name-name] .Jason". * For example, if this method is * & lt; Code & gt; testSomeBehavior & lt; / code & gt; Called a test method, the resource name will be loaded & lt; Code & gt; TestSomeBehavior.json & lt; / code & gt; * / Protected string load content thrusts () IOException throws {return loadResource (testName.getMethodName () + ".json"); } / ** * Loads an XML resource whose name is currently being taken from the * test * Derived derived name is "[test-method-name] .xml" * For example, if this method To * & lt; Code & gt; TestSomeBehavior & lt; / code & gt; Called a test method, the resource name will be loaded & lt; Code & gt; TestSomeBehavior.xml & lt; / code & gt; * / Protected string load content test (XML) throws IOException {return loadResource (testName.getMethodName () + ".xml"); }} then in the sub-class:
public static zero main (string [] args) {loadSetupMessage ("myJsonfile.json"); } Secure zero loadetup message throws IOException (string filename) {string raw message = this.loadResource (fileName); System.out.println (String.format ("loaded class% s", raw message);} and keep myJsonfile.json in the same package hierarchy.
Comments
Post a Comment