0

Can I host a restful service in a main method? It doesn't have to be Jackson.

I looked around for examples, but couldn't find any. I remember that for Servlets I need a web server, but it would be awesome if I can host a web service in a main method. It's just a simple method.

The data is static (imagine a hashtable with 900k elements).It is loaded from a json file (long story).

The method serves one element from the hash table by key.

mist
  • 1,853
  • 2
  • 19
  • 33

2 Answers2

1

Have you tried using Java Restlets?

https://restlet.com/technical-resources/restlet-framework/tutorials/2.3

Gnana
  • 614
  • 1
  • 7
  • 18
  • 1
    No. Never heard of it. But after investigating - it's my loss. I'm checking it out. Can I start it from a main method? – mist Oct 18 '16 at 21:37
  • I think so. I haven't had the need to try it personally. I will try from my end as well :) – Gnana Oct 19 '16 at 18:48
1

If you really want to go java route, take a look here. https://stackoverflow.com/a/3732328/6785908

Read from the json file and write it to the output stream in the handle method (Instead of String response = "This is the response";)

I would rather use nginx or httpd as the static file server.

Edit

"The method serves one element from the hash table by key." So, at this point I would strongly encourage you to use spring boot. It has an embedded servlet container, you can just run the main method (or java -jar .jar, if you have packaged it in jar) Added advantage: if you have to marshall an object to json using jackson, spring boot comes with it out of the box

Community
  • 1
  • 1
so-random-dude
  • 15,277
  • 10
  • 68
  • 113
  • The data is static - imagine a large hashtable that is static. the service will serve 1 element from the hashtable. I'll update the question. sorry for the confusion – mist Oct 18 '16 at 21:16