还是REST的新手,但接近完成我的第一个程序。
我正在尝试创建一个CSV,每次POST时都会将POST URL参数的值传递给它。
有没有办法将我的变量设置为等于我的POST URL参数?
下面的代码(当我想出来时,值暂时设置为10):
@Path("/values")
public class values {
int totalSum = 0;
List<String> list = new ArrayList<String>();
@GET
@Produces(MediaType.TEXT_PLAIN)
public int getSum() throws IOException {
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
List<String[]> read = reader.readAll();
return sum.sum(read, totalSum);
}
@POST
public String addValue() throws IOException {
int value = 10;
String valueString = Integer.toString(value);
CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv", true), ',');
String[] entries = valueString.split(",");
writer.writeNext(entries);
writer.close();
return "ok";
}
@DELETE
@Produces(MediaType.TEXT_PLAIN)
public String deleteList() throws IOException {
FileWriter fw = new FileWriter("yourfile.csv", false);
PrintWriter pw = new PrintWriter(fw, false);
pw.flush();
pw.close();
fw.close();
return "ok";
}
}
答案 0 :(得分:0)
现在您需要基本网址,当您添加/ {some_value}时,它会通过PathVariable
映射到字符串值test
喜欢这样吗?
@Path("/{test}")
@POST
// Javax.ws.rs.PathParam
public String addValue(@PathParam("test") final String test) throws
// SpringFramework
//public String addValue(@PathVariable("test") final String test) throws IOException {
final String value = test;
CSVWriter writer = new CSVWriter(new FileWriter(value, true), ',');
String[] entries = valueString.split(",");
writer.writeNext(entries);
writer.close();
return "ok";
}
有关详细信息,请参阅link