Reputation: 106350
For the last couple of years I've had my head in Python, where there are numerous choices for simple, minimal frameworks that allow me to stand up a website or service easily (eg. web.py). I'm looking for something similar in Java.
What is the simplest, least-moving-parts way of standing up simple services using Java these days? I'm looking for something as simple as:
Bonus points if the framework plays well with Jython.
[Update] Thanks for the responses, some of these look quite interesting. I'm not seeing the url dispatch capability in these, however. I'm looking for something similar to Django's url.py system, which looks like:
urlpatterns = patterns('',
(r'^articles/2003/$', 'news.views.special_case_2003'),
(r'^articles/(\d{4})/$', 'news.views.year_archive'),
(r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'),
(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'),
)
Where you specify a url regular expression along with the handler that handles it.
Upvotes: 6
Views: 5737
Reputation: 339372
Java 18+ comes with a basic HTTP server. See JEP 408: Simple Web Server.
By “basic” I mean no CGI or servlet-like functionality, no TLS/SSL support.
You can use it as a command-line tool on a console. Or you can invoke it within a Java app. While meant primarily to be a static web server, an API is available to you from within Java.
What you likely really need is the Jakarta Servlet framework in Jakarta EE, formerly known as Java Servlet in Java EE.
This framework provides simple hooks for you to tap into every request coming and the response going out. You simply write code to build the content to be delivered to the user.
You need not know anything about the web server itself at runtime, as your Java web app (your Servlet(s) that you wrote) can run in any web server runtime (“container”) that complies with the Jakarta Servlet specification. You have a choice of many products implementing the Jakarta Servlet specification, with Apache Tomcat and Eclipse Jetty being two of the most popular.
Jakarta Servlet meets all three of your bullet items.
Spring Boot is a project to embed a Servlet-compliant container along with your Servlet code, compbined into a single executable app. This contrasts with the classic approach of running the container as a server, and dropping your Servlet code into it.
Upvotes: 0
Reputation: 16393
Jetty is a pretty nice embedded http server - even if it isn't possible to do the mapping like you describe, it should be pretty easy to implement what you are going for.
Upvotes: 0
Reputation: 116382
there are several alternatives:
all these frameworks come with a built-in server.
EDIT
jax-rs has a similar approach using url templates:
@Path("/users/{username}")
public class UserResource {
@GET
@Produces("text/xml")
public String getUser(@PathParam("username") String userName) {
}
}
then put your handlers in an Application object:
public class MyApplicaton extends Application {
public Set<Class> getClasses() {
Set<Class> s = new HashSet<Class>();
s.add(UserResource.class);
return s;
}
}
another example with JAX-RS:
@GET
@Produces("application/json")
@Path("/network/{id: [0-9]+}/{nid}")
public User getUserByNID(@PathParam("id") int id, @PathParam("nid") String nid) {
}
EDIT 2
Restlet supports a centralized configurations like Django, in your Application object:
// Attach the handlers to the root router
router.attach("/users/{user}", account);
router.attach("/users/{user}/orders", orders);
router.attach("/users/{user}/orders/{order}", order);
Upvotes: 3
Reputation: 199264
I've hard about: Apache Mina
But quite frankly I don't even know if it is what you need.
:-/
:)
Upvotes: 0
Reputation: 20334
Note: This is more general discussion than answer.
I'm having similar issues coming from Python for 10+ years and diving, as it were, back into Java. I think one thing I'm learning is that the "simplicity" factor of Python is very different from that of Java. Where Python abounds with high-level framework-- things like web.py, Java seems much more lower level. Over the past few months, I've gone from saying "What's the Java way to do this easy in Python thing" to "How does one build up this thing in Java." Subtle, but seems to bring my thoughts around from a Python-centric view to a more Java-centric one.
Having done that, I've realized that standing up a website or service is not simple for a Java outsider, that's because there's a large amount of info I have to (re)grok. It's not as simple as python. You still need a webserver, you need to build a "container" to drop your Java code into, and then you need the Java code (am I wrong on this, everyone? Is there a simpler way?).
For me, working with Scala and Lift has helped- and not even those, but this one thread by David Pollack. This was what I needed to build a Jetty server. Take that, follow the directions (somewhat vague, but might be good enough for you) and then you have a servlet container ready to accept incoming traffic on a port (or 3 ports, in his case). Then you can write some Java code using HTTPServlet or something to go the rest of the way.
Again, this is just what I did to get past that barrier, but I'm still not a Java guru. Good luck.
Upvotes: 1
Reputation: 4540
Servlets might be the way to go. To do very simple things you only need to override one method of one class. More complicated stuff is of course possible, but you can go a long way with a little work.
Investigate Tomcat or Jetty - both are open source and well supported.
public class HelloWorldServlet extends HttpServlet {
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
{
response.setContentType( "text/plain" );
PrintWriter out = response.getWriter();
out.print( "hello world!" );
}
}
Upvotes: 1
Reputation: 44215
I liked to worth the Simple HTTP Server from the Simple Framework. It offers a nice Tutorial about how to start as well.
Upvotes: 3