Java 如何使用RESTfulWebServices从文件中读取所有值并生成相关响应
在客户端类中,我硬编码int inch=12;int英尺=2;但我的要求是必须 创建具有若干英寸和英尺的单独文件。我想从中获取所有英寸和英尺值 文件及其相关响应 我的客户代码: 公共类临时客户端{Java 如何使用RESTfulWebServices从文件中读取所有值并生成相关响应,java,web-services,rest,tomcat,Java,Web Services,Rest,Tomcat,在客户端类中,我硬编码int inch=12;int英尺=2;但我的要求是必须 创建具有若干英寸和英尺的单独文件。我想从中获取所有英寸和英尺值 文件及其相关响应 我的客户代码: 公共类临时客户端{ static final String REST_URI = "http://localhost:8081/CustomerDemo"; static final String INCH_TO_FEET = "/ConversionService/InchToFeet/";
static final String REST_URI = "http://localhost:8081/CustomerDemo";
static final String INCH_TO_FEET = "/ConversionService/InchToFeet/";
static final String FEET_TO_INCH = "/ConversionService/FeetToInch/";
public static void main(String[] args) {
int inch=12;
int feet=2;
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(REST_URI);
WebResource addService = service.path("rest").path(INCH_TO_FEET+inch);
System.out.println("INCH_TO_FEET Response: " + getResponse(addService));
System.out.println("INCH_TO_FEET Output as XML: " + getOutputAsXML(addService));
System.out.println("---------------------------------------------------");
WebResource subService = service.path("rest").path(FEET_TO_INCH+feet);
System.out.println("FEET_TO_INCH Response: " + getResponse(subService));
System.out.println("FEET_TO_INCH Output as XML: " + getOutputAsXML(subService));
System.out.println("---------------------------------------------------");
}
private static String getResponse(WebResource service) {
return service.accept(MediaType.TEXT_XML).get(ClientResponse.class).toString();
}
private static String getOutputAsXML(WebResource service) {
return service.accept(MediaType.TEXT_XML).get(String.class);
}
}
假设您有一个名为data.txt的文件,比如
1
2
3
4
5
6
7
你可以这样做
BufferedReader br = new BufferedReader(new FileReader(new File("C:\\Users\\Leo\\workspace\\STackOverflow\\src\\data.txt")));
String line = null;
List<Integer> inches = new ArrayList<Integer>();
while((line = br.readLine())!=null){
inches.add(Integer.parseInt(line));
}
br.close();
for(Integer inch:inches){
WebResource addService = service.path("rest").path(INCH_TO_FEET+inch);
System.out.println("INCH_TO_FEET Response: " + getResponse(addService));
System.out.println("INCH_TO_FEET Output as XML: " + getOutputAsXML(addService));
System.out.println("---------------------------------------------------");
}
然后你也可以对脚做同样的操作那么,你是在问我们如何读取文件,对吗?Google for Java IO教程,单击第一个链接,阅读,然后尝试一些东西。