50

Is there any simple java web framework like sinatra (for ruby) or web.py (for python)?

Jonas
  • 97,987
  • 90
  • 271
  • 355
Gabriel
  • 525
  • 1
  • 5
  • 3

14 Answers14

52

If you want a strict Java framework Spark might be an alternative:

import static spark.Spark.*;

public class HelloWorld {
   public static void main(String[] args) {
      get("/hello", (req, res) -> "Hello World");
   }
}
pka
  • 563
  • 4
  • 9
  • 3
    Play looks awesome, but it looks like a full stack framework. Sinatra is not intended to be a full stack framework, and Spark looks much closer to the simplicity of Sinatra. – Jeff Wigal May 19 '12 at 12:57
  • 3
    Spark also supports Java 8 Lambdas: get("/hello", (request, response) -> { .. }); – micha May 31 '14 at 13:57
  • 1
    Spark doesn't support any type of dependency injection or IoC container support which severely limits its use IMO. – TheJediCowboy Aug 19 '15 at 04:15
  • I would consider it an advantage that Spark does not contain dependency injection and any of that magic crap – nyholku Apr 09 '21 at 12:47
22

Play. Haven't tried it myself but heard only good things about it and seems to be quite beginner-friendly.

helpermethod
  • 51,037
  • 60
  • 165
  • 263
  • 3
    play is pretty cool. I was quite impressed by the tutorial. – Andreas Dolk May 28 '10 at 09:12
  • 2
    A friend of mine played with it for a while. He seemed to be satisfied with it. – Thomas Owens Jul 17 '10 at 16:03
  • 6
    Play is fantastic - I've used in on several projects. Important advantages / simplifications that Play offers: 1. Self-contained web server. No need to deploy as a WAR to a separate web container (like Tomcat) 2. MVC without servlets 3. Change source code, save, refresh browser - no need to deploy a new war. = very fast development. 4. Just very simple and easy to learn 5. Uses ORM (Hibernate by default) for simplifying database interaction – Mark S Jul 17 '10 at 16:05
  • 3
    Play is based around the concept of controllers and a routes configuration file. Sinatra is based around an internal DSL for defining handlers for verbs and URLs. Not a very good match. http://www.sparkjava.com/ seems to be a closer fit, but I can't attest to it's maturity. – Johannes Brodwall Jan 31 '12 at 16:08
  • In Play java is a second citizen. It also breaks the rules of J2EE developing. Definitely don't recommend it – Kyle Luke Jan 21 '16 at 15:00
  • @KyleLuke While this true nowadays Java was a first class citizen when Play was at version 1. – helpermethod Jan 21 '16 at 19:54
17

I think the simplest thing to do to generate web content via Java is to write a Servlet. Just like web.py allows you to define a GET method, you can implement a Servlet's doGet() method and write data directly back to the client.

Here is a link to the Servlets tutorial. You will also need to know how to package and deploy a web application; for that I usually point people to the Tomcat manual (see the section titled "First Web Application").

Writing and deploying a Java web application is not going to be as fast as in Ruby or Python, but Java isn't particularly known for its succinctness.

If you don't strictly require Java, check out Grails. It's a web application framework built on Groovy, which is a dynamic language similar to Python and Ruby that compiles to the JVM.

danben
  • 73,814
  • 18
  • 117
  • 141
  • 1
    + 1 for Grails. I would recommend check out Roo project too – Perpetualcoder Jan 24 '10 at 01:52
  • 3
    J2EE is dead. Long live Java EE. Here's the right link to the Servlets tutorial: http://java.sun.com/javaee/5/docs/tutorial/doc/bnafd.html – BalusC Jan 24 '10 at 04:11
  • 3
    Tomcat is no where near a simple as web.py –  May 17 '10 at 05:11
  • 4
    The simplicity of servlets ends after the bit where you write code. You then have to package them up in an annoying bundle file, and run an annoying and overly complicated J2EE servlet container which comes with a space shuttle cockpit's worth of switches and dials. Although possibly you can use standalone Jetty, that still has far too many options. – Julian Morrison Apr 20 '12 at 09:53
12

JAX-RS.

Java EE 6 Servers like GlassFish bundles it by default.

If you use Tomcat, you can use Jersey, Apache CXF, or Restlet implementations.

Using JAX-RS annotations the web development feels like Sinatra or Merb. BTW you don't have to use Java as the language, you can use Scala, Groovy, JRuby...

Hendy Irawan
  • 17,676
  • 10
  • 94
  • 105
7

If you are only looking for a presentation framework in pure Java then, for me, Stripes1 is the closest of the Java MVC frameworks to the RoR philosophy: simple, elegant, and requiring minimal configuration.

1 Stripes pioneered the Convention over Configuration approach for Java web development. And while some other frameworks have adopted some of its principles (like Spring MVC or Struts2 with plugins), I still prefer Stripes because it does one thing, and does it well.

Pascal Thivent
  • 535,937
  • 127
  • 1,027
  • 1,106
  • 1
    Have you used it professional? Does it fit well with ajax and javascript? – darpet Oct 02 '10 at 21:30
  • 1
    @darko Yes, and it has been a pleasure to work with. Regarding AJAX and JavaScript, have a look at the [AJAX](http://www.stripesframework.org/display/stripes/AJAX) section and the various [User Addition](http://www.stripesframework.org/display/stripes/User+Additions) on the official website, or the [The Stripes Book Blog](http://www.stripesbook.com/blog/) for more examples. – Pascal Thivent Oct 02 '10 at 21:46
7

It is possible to use Sinatra as is with JRuby

Matthieu
  • 4,415
  • 4
  • 36
  • 56
Yuval Rimar
  • 975
  • 11
  • 20
5

look at this two as well: 1. activeweb and 2. dropwizard

joshua
  • 3,928
  • 3
  • 37
  • 53
3

If you have to develop business or database applications OpenXava is a good option. OpenXava allows you to develop a complete AJAX web application writing just domain classes, with no code generation and producing an application ready for production. Little code, great result.

  • 2
    Can you suggest a tutorial for this? – Gapchoos Oct 11 '12 at 05:40
  • 1
    there is a quickstart tutorial on the [OpenXava website](http://www.openxava.org/web/guest/home) – Chris Snow Nov 09 '12 at 20:14
  • 1
    @Javier , You need to add that OpenXava applications are deployed as portlets in a portal server such as Liferay. Its also not easy to create a REST API in an OpenXava application. – joshua Mar 10 '13 at 17:29
3

Check SerfJ : Simplest Ever Rest Framework for Java :

Using SerfJ is the easiest way of developing Java REST web applications. It helps you to develop your application over an elegant MVC arquitecture, giving more importance to convention than configuration, so for example, you will not have to have configuration files or annotations in order to specify which view serves a controller's method. However, SerfJ is very flexible library, so if you want to jump over those conventions, you can configure the behaviour of your applications as you like.

The framework tries to meet JSR 311 specification, but it doesn't follow every point of that, because the purpose is to have a very intuitive library, and some some aspects of the specification are out of the scope of SerfJ.

SerfJ is opensource and is released under the Apache License, Version 2.0.

Matthieu
  • 4,415
  • 4
  • 36
  • 56
Yossarian
  • 31
  • 1
3

You might want to have a look at those 2 groovy projects:

https://github.com/webdevwilson/graffiti

https://github.com/bleedingwolf/Ratpack

Really light akin to Sinatra. Might be a bit on the bleeding edge though:-) Interesting and promising never the less.

pdeschen
  • 1,161
  • 13
  • 17
2

The smallest "usable" web server for Java that support Servlets that I can find is Miniature JWS. And honestly there is no reason to run a Java web server that doesn't support Servlets. If you want to do REST, Restlet has a built in HTTP daemon that means you can run it as a stand alone web server.

2

HybridJava framework is really simple. Of course, it builds like an API above Servlet API, but deals with page and component instead of request and response. In other words it is truly MVC.

Matthieu
  • 4,415
  • 4
  • 36
  • 56
Alex
  • 87
  • 3
1

Step is a framework for Scala inspired by Sinatra.

Ken Liu
  • 21,056
  • 17
  • 71
  • 96
1

I can recommend Struts2 to you, because i like the plugin architecture and with the convention plugin it is simple and fast to develop.

Johannes
  • 2,070
  • 3
  • 18
  • 27