简体   繁体   English

简单的Java独立服务器容器/框架?

[英]Simple Java stand-alone server container/framework?

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). 在过去的几年里,我已经掌握了Python,其中有许多简单,最小框架的选择,这些框架允许我轻松地建立网站或服务(例如web.py)。 I'm looking for something similar in Java. 我正在寻找类似Java的东西。

What is the simplest, least-moving-parts way of standing up simple services using Java these days? 如今,使用Java提供简单服务的最简单,最不移动的部分是什么? I'm looking for something as simple as: 我正在寻找一些简单的东西:

  • the ability to receive HTTP requests 接收HTTP请求的能力
  • the ability to dispatch those requests to handlers (preferably a regular expression based url to handler mapping facility) 能够将这些请求分派给处理程序(最好是基于正则表达式的url到处理程序映射工具)
  • the ability to set HTTP headers and generally fully control the request/response 能够设置HTTP标头并通常完全控制请求/响应

Bonus points if the framework plays well with Jython. 如果框架与Jython一起运行,奖励积分。

[Update] Thanks for the responses, some of these look quite interesting. [更新]感谢您的回复,其中一些看起来很有趣。 I'm not seeing the url dispatch capability in these, however. 但是,我没有看到这些中的url调度功能。 I'm looking for something similar to Django's url.py system, which looks like: 我正在寻找类似于Django的url.py系统的东西,它看起来像:

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. 在哪里指定url正则表达式以及处理它的处理程序。

I liked to worth the Simple HTTP Server from the Simple Framework. 我喜欢简单框架中的Simple HTTP Server。 It offers a nice Tutorial about how to start as well. 它提供了一个关于如何开始的好教程

there are several alternatives: 有几种选择:

  • servlets 小服务程序
  • restlet : lightweight REST framework restlet :轻量级REST框架
  • jax-rs: using jersey or the restlet module implementing the jax-rs specs jax-rs:使用jersey或实现jax-rs规范的restlet模块
  • grizzly : NIO based server (with HTTP support + handlers) grizzly :基于NIO的服务器(具有HTTP支持+处理程序)
  • apache mina : event-driven, async server (with HTTP support) apache mina :事件驱动的异步服务器(支持HTTP)

all these frameworks come with a built-in server. 所有这些框架都带有内置服务器。

EDIT 编辑

jax-rs has a similar approach using url templates: jax-rs使用url模板有类似的方法:

@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: 然后将处理程序放在Application对象中:

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: 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 编辑2

Restlet supports a centralized configurations like Django, in your Application object: Restlet在您的Application对象中支持像Django这样的集中式配置:

// 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);  

Servlets might be the way to go. Servlets可能是最佳选择。 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. 调查TomcatJetty - 两者都是开源的并得到很好的支持。

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!" );
    }
}

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. 我有类似的问题来自Python 10年以上,并且正在潜水,回到Java。 I think one thing I'm learning is that the "simplicity" factor of Python is very different from that of Java. 我认为我正在学习的一件事是Python的“简单”因素与Java的非常不同。 Where Python abounds with high-level framework-- things like web.py, Java seems much more lower level. Python充斥着高级框架 - 比如web.py,Java似乎要低得多。 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." 在过去的几个月里,我已经说过“用Python做什么简单的Python方法”到“如何用Java构建这个东西”。 Subtle, but seems to bring my thoughts around from a Python-centric view to a more Java-centric one. 微妙,但似乎将我的想法从以Python为中心的视图转变为更加以Java为中心的视图。

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. 完成后,我意识到站在一个网站或服务对于一个Java局外人来说并不简单,那是因为我需要(重新)了解大量信息。 It's not as simple as python. 它不像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?). 您仍然需要一个Web服务器,您需要构建一个“容器”来删除您的Java代码,然后您需要Java代码(我错了,每个人?有更简单的方法吗?)。

For me, working with Scala and Lift has helped- and not even those, but this one thread by David Pollack . 对我来说,与Scala和Lift一起工作有帮助 - 甚至不是那些,但David Pollack的这一个主题 This was what I needed to build a Jetty server. 这是我构建Jetty服务器所需要的。 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). 接下来,按照指示(有点模糊,但可能对你来说足够好)然后你有一个servlet容器准备好接受端口上的传入流量(或者在他的情况下是3个端口)。 Then you can write some Java code using HTTPServlet or something to go the rest of the way. 然后你可以使用HTTPServlet编写一些Java代码,或者继续使用其他东西。

Again, this is just what I did to get past that barrier, but I'm still not a Java guru. 再一次,这正是我为了克服这个障碍所做的,但我仍然不是Java大师。 Good luck. 祝好运。

I've hard about: Apache Mina 我很难: Apache Mina

But quite frankly I don't even know if it is what you need. 但坦率地说,我甚至不知道这是否是你所需要的。

:-/ : - /

:) :)

Jetty是一个非常好的嵌入式http服务器 - 即使你不能像你描述的那样进行映射,它应该很容易实现你的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM