I am developing an Dynamic Web Application with Eclipse. I have e working MySQL-Database which is connected over a class called 'Data Access Object' (=DAO) that works with JDBC. I want to create entries into this database. The functions are ready. With ready I mean tested and OK. On the same application I implemented Java Jersey's RESTful WebService. It is working well, I can call the service and it returns my desired information. But now to my question:
How can I send a String containing XML? The String has to be parsed in the WebMethod to build and execute the query.
My WebService looks as follows:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
@Path("/input")
public class Input {
//DAO instance to have connection to the database.
//Not used yet.
//private DAO dao = new DAO();
@PUT
@Consumes(MediaType.TEXT_XML)
@Path("/result")
public void putIntoDAO(InputStream xml) {
String line = "";
StringBuilder sb = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(xml));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(sb.toString());
}
}
As you see I tried to print the incoming Stream to the console. I repeat the most important things:
- I know how to parse XML.
- I know my DAO works properly.
- I know my WebService works as well.
What I would like to know:
- How do I send an XML-String to my WebService?
- How do I access this String in my PUT-method?
Thank you for your attention and try to help me. I appreciate even every try to.
Kind regards
L.