-2

Still super new to REST, but close to finishing my first program.

I am attempting to create a CSV with the value of the POST URL Parameter being passed into it each time I POST.

Is there a way of setting my variable to be equal to my POST URL Parameter?

Code below (value is temporarily set to 10 while I figure this out):

@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";
}

}
Narf
  • 14,600
  • 3
  • 37
  • 66
Intrepid Diamond
  • 462
  • 1
  • 6
  • 16

1 Answers1

0

So now you need the base url, and when you add /{some_value} it gets mapped via PathVariable to String value test

Like so?

@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";  
}  

See link for more information

Danielson
  • 2,605
  • 2
  • 28
  • 51