5 ms·
Big fan of sparkjava for no-nonsense HTTP services. The days of tomcat-for-everything were not fun as someone who only occasionally dabbled in java back then.
by Nursie 7y ago
Big fan of sparkjava for no-nonsense HTTP services. The days of tomcat-for-everything were not fun as someone who only occasionally dabbled in java back then.
So I have to build this as a special type of jar and then set up this other, rather opaque, set of stuff and deal with all sorts of nonsense ...
Now?
get("/hello", (a,b) -> "Hello World");
And we're done...
- butteroverflow 7y agoThat sure looks nice, but how often do you write services which return a single constant string? I like to judge languages/frameworks based on more representative snippets.
- unscaled 7y agoI think this is not about showing off the easy case, but rather the straightforwardness of the framework. I haven't used spark myself, but I used frameworks quite like it (Jooby, Rapidoid, Ktor) in Java, as well their counterparts in Nodejs (Express, Koa) and Go ("net/http" + some router). The applications certainly got a lot more complex than that, but we never found ourselves in need of a magic annotation that you can't step through with your debugger. This is all about simplicity. It's pretty clear how a get() function on the router which takes a route and a callback works, but it requires some work to understand how Spring magically scans all your classes looking for annotations and then builds parser based on that. It's quite unclear where all of the routes are coming from (since they can come from a class just about anywhere). You suddenly need special tools. And the worst thing that suffers is transparency. Ctrl-clicking/Cmd-clicking on an annotation in your favourite IDE never tells you what it does. In comparison, a second-order function is a vastly simpler and more grkoable beast.
- Nursie 7y ago> That sure looks nice, but how often do you write services which return a single constant string? The point was just to show that the http server aspect of it is trivial to set up. You can hook in whatever you want from there.
- Nursie 7y agoFYI, the "(a, b) -> Hello World" piece is a lambda returning a string constant, rather than just a string constant, hence the ability to hook in whatever you want.
- nurettin 7y agoSince we are hiding the rest of the app, in spring boot + kotlin that would be: @GetMapping("/hello") fun hello() = "Hello, World!"
- unscaled 7y agoThat's a nice try, but the rest of the app would be[1]: import static spark.Spark.get; public class HelloWorld { public static void main(String[] args) { get("/hello", (req, res) -> "Hello World"); } } [1] http://sparkjava.com/ http://sparkjava.com/
- nurettin 7y agoIt is indeed a nice try, and the rest of the app would be: @SpringBootApplication @RestController class DemoApplication{ @GetMapping("/hello") fun hello() = "Hello, World!" } fun main(args: Array<String>) { runApplication<DemoApplication>(*args) } And this taught me that the best sinatra clone in java isn't really worth it in terms of lines of code saved (we save what? 3 lines of code? 6 with the imports?) when compared to a full-blown web framework like spring boot. Would you care to do another one where we return a json response? I'd bet you will need a json mapper class for complex objects whereas I will return a dataclass instance and it will just work.
- unscaled 7y agoIn Kotlin, so you don't have to define multiple files, obviously skipping the imports. Let's do the same with Jooby, for instance: fun main(args: Array<String>) = run(*args) { get { "Hello Kotlin" } } It's always easy to make a framework lightweight. But there is no argument that Spring requires a lot of tedious boilerplate in Java (some of which can be avoided with Kotlin), and annotations which are hard to understand. Lightweight frameworks don't really tell you "We do everything that Spring does in less lines", since at one point you'll ask them to make avocado toasts or something else that spring-bakery has a plugin for. The real power of lightweight frameworks is their simplicity. This is not for everyone (that's why we've got Spring and Rails), but if everybody was fine with the tradeoffs that Spring makes, you'll see the same kind of frameworks ruling the Node.js and Go landscape.
- lihaoyi 7y agoIn Scala using Cask it's object MinimalApplication extends cask.MainRoutes{ @cask.get("/") def hello() = "Hello World!" initialize() } API design shamelessly stolen from Python's Flask framework