Reputation: 6059
We have a JSON Schema definition file hosted in a server and accessible via an URL. My Spring Controller, needs to validate the incoming JSON data (from an Ajax Call) against this schema.
Given the URL (like.. http://myservices.json.apiOneSchema.json) of the schema definition file, How can i read this schema definition from my Spring Controller ?
Upvotes: 1
Views: 4840
Reputation: 3057
Spring provides Resource
class using which it can be done
public static void main( String[] args )
{
ApplicationContext appContext =
new ClassPathXmlApplicationContext(new String[] {"If-you-have-any.xml"});
Resource resource =
appContext.getResource("http://www.common/testing.txt");
try{
InputStream is = resource.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}catch(IOException e){
e.printStackTrace();
}
}
The above snippet does exactly what you require. Also once you read the Resource you can use any binding to directly convert them to your POJO. The above is just an example of how you can read it.
Upvotes: 3