gpt4 book ai didi

java - 如何使用 Spring Controller 从 URL 读取文件?

转载 作者:行者123 更新时间:2023-11-29 05:05:43 24 4
gpt4 key购买 nike

我们有一个托管在服务器中的 JSON 架构定义文件,可通过 URL 访问。我的 Spring Controller 需要根据此模式验证传入的 JSON 数据(来自 Ajax 调用)。

给定架构定义文件的 URL(如.. http://myservices.json.apiOneSchema.json),我如何从我的 Spring Controller 读取此架构定义?

最佳答案

Spring 提供了Resource 类来完成

 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();
}

}

上面的代码片段完全符合您的要求。此外,一旦您阅读了资源,您就可以使用任何绑定(bind)将它们直接转换为您的 POJO。以上只是您如何阅读它的示例。

关于java - 如何使用 Spring Controller 从 URL 读取文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30431750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com