I am following a tutorial to develop a RESTful web service in java based on JAX-RS. I have modified the POST
method in order to upload a file to service from client (see code below). In tutorial, a .WAR
package is deployed into tomcat apache server.
My application is very simple, just to use a POST
method. I have only one client, no user management is needed. RESTful is stateless so no caching is needed. To me, a full fledge tomcat seems to be redundant.
I went through different answers here already embedded-server and server-2 and they suggested to make a main method in Java which will listen on a certain port using jax-ws
.
javax.xml.ws.Endpoint.publish("http://localhost:8000/myService/", myServiceImplementation);
I suspect that this simple solution will go wrong, may be I miss some security related things? Can it deteriorate the reliability of the services? etc. can somebody explain what can go wrong if I use a simple solution instead of full fledge tomcat?
@Path("/file")
public class RESTfulHelloWorld
{
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail)
{
//String uploadedFileLocation = "d:/uploaded/" + fileDetail.getFileName();
String uploadedFileLocation = "d:/test.txt";
// save it
writeToFile(uploadedInputStream, uploadedFileLocation);
String output = "File uploaded to : " + uploadedFileLocation;
return Response.status(200).entity(output).build();
}
private void writeToFile(InputStream uploadedInputStream,
String uploadedFileLocation)
{
try {
OutputStream out = new FileOutputStream(new File(
uploadedFileLocation));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}