Hi, I'm trying to create a Fantom code using a java library to implement a simple example using NanoHTTP Library. This is a nano library to create web applications. In their github project (https://github.com/NanoHttpd/nanohttpd) the Hello World example is this:
public class App extends NanoHTTPD {
public App() throws IOException {
super(8080);
start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
System.out.println("\nRunning! Point your browsers to http://localhost:8080/ \n");
}
public static void main(String[] args) {
try {
new App();
} catch (IOException ioe) {
System.err.println("Couldn't start server:\n" + ioe);
}
}
@Override
public Response serve(IHTTPSession session) {
return newFixedLengthResponse("<html><body>Hello World</body></html>");
}
}
And i created a Fantom version to get the same idea:
using [java] fi.iki.elonen::NanoHTTPD as NanoHTTPD
using [java] fi.iki.elonen::NanoHTTPD$Response as Response
using [java] fi.iki.elonen::NanoHTTPD$IHTTPSession as IHTTPSession
class App : NanoHTTPD {
new make() : super(8080) {
start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
echo("\nRunning! Point your browsers to http://localhost:8080/ \n");
}
static Void main() {
try {
App.make
}
catch (IOErr ex) {
echo("Couldn't start server:\n")
}
}
override public Response serve(IHTTPSession session) {
return newFixedLengthResponse("<html><body>Hello World</body></html>")
}
}
And i'm facing this error: : Cannot override Java overloaded method: serve ERROR: cannot compile script
I don't know what i miss and still newbie in Fantom. Sorry about formatting.
Thanks in advance!
SlimerDudeWed 14 Aug 2019
Your code looks fine. I think the compilation error tells the problem:
Looking at the Fantom Java FFI docs, it says this:
Features which are not yet available in Java FFI:
- Attempting to override a Java overloaded method; this means you cannot subclass or extend from a type with abstract overloaded methods
So it doesn't look like you can override the serve() methods - but it looks like that has been deprecated anyway - but it is interesting that I can not see any other serve() method in the code!??
Anyway, try overriding the handle() method instead.
Is there any reason why you're using NanoHTTP over the core Fantom Wisp web server?
omarptaWed 14 Aug 2019
Hi Slime, Thanks for your help! I was trying to use NanoHTTPD because already have same projects using it. But will give a try to Fantom Wisp and it seems to be the tool that i need.
SlimerDudeWed 14 Aug 2019
No worries.
As a starting point you may want to try BedSheet (which runs on top of wisp) to serve static files - if that's what you need?
Cool! I'm doing just some json rest api server. Wisp is very good. Do you know if exist a Rest framework that runs on top of wisp?
SlimerDudeWed 14 Aug 2019
Hi omarpta, there's no specific REST library but BedSheet handles it pretty well - see BedSheet RESTful Services.
I myself go one step further and use the Pillow library that runs on top of BedSheet - see Pillow RESTful Services.
With Pillow, to service a PUT request at /api/create/{id} you would write:
using afIoc
using afBedSheet
using afPillow
@Page
const mixin ApiPage {
@Inject abstract HttpRequest httpRequest
@Inject abstract HttpResponse httpResponse
@PageEvent { httpMethod="PUT" }
Text onCreate(Int id) {
// get submitted data as a JSON object
json := httpRequest.body.jsonObj
...
// return a different status code, e.g. "201 - Created"
httpResponse.statusCode = 201
// return JSON objects to the client
return Text.fromJsonObj(["response":"OK"])
}
}
There are more comprehensive articles on Alien-Factory, such as:
This is exactly what I need and with JSON integration. I was reading BedSheet docs and links examples... It seems to be very easy like your example. Will play a lot on this. Thanks!
omarpta Wed 14 Aug 2019
Hi, I'm trying to create a Fantom code using a java library to implement a simple example using NanoHTTP Library. This is a nano library to create web applications. In their github project (https://github.com/NanoHttpd/nanohttpd) the Hello World example is this:
And i created a Fantom version to get the same idea:
using [java] fi.iki.elonen::NanoHTTPD as NanoHTTPD
using [java] fi.iki.elonen::NanoHTTPD$Response as Response
using [java] fi.iki.elonen::NanoHTTPD$IHTTPSession as IHTTPSession
class App : NanoHTTPD {
}
And i'm facing this error: : Cannot override Java overloaded method:
serve
ERROR: cannot compile scriptI don't know what i miss and still newbie in Fantom. Sorry about formatting.
Thanks in advance!
SlimerDude Wed 14 Aug 2019
Your code looks fine. I think the compilation error tells the problem:
Looking at the Fantom Java FFI docs, it says this:
So it doesn't look like you can override the
serve()
methods - but it looks like that has been deprecated anyway - but it is interesting that I can not see any otherserve()
method in the code!??Anyway, try overriding the handle() method instead.
Is there any reason why you're using NanoHTTP over the core Fantom Wisp web server?
omarpta Wed 14 Aug 2019
Hi Slime, Thanks for your help! I was trying to use NanoHTTPD because already have same projects using it. But will give a try to Fantom Wisp and it seems to be the tool that i need.
SlimerDude Wed 14 Aug 2019
No worries.
As a starting point you may want to try BedSheet (which runs on top of wisp) to serve static files - if that's what you need?
See here for a simple example: Write a Web Server in 7 Lines of Code!
omarpta Wed 14 Aug 2019
Cool! I'm doing just some json rest api server. Wisp is very good. Do you know if exist a Rest framework that runs on top of wisp?
SlimerDude Wed 14 Aug 2019
Hi
omarpta
, there's no specific REST library but BedSheet handles it pretty well - see BedSheet RESTful Services.I myself go one step further and use the Pillow library that runs on top of BedSheet - see Pillow RESTful Services.
With Pillow, to service a
PUT
request at/api/create/{id}
you would write:There are more comprehensive articles on Alien-Factory, such as:
omarpta Wed 14 Aug 2019
This is exactly what I need and with JSON integration. I was reading BedSheet docs and links examples... It seems to be very easy like your example. Will play a lot on this. Thanks!