【问题标题】:Creating a java server with rest用rest创建一个java服务器
【发布时间】:2011-12-17 00:07:44
【问题描述】:

我需要创建一个休息服务器和客户端。

我偶然发现了这个使用套接字的tutorial。我希望能够使用 REST 调用,可能是 HTTP,因为客户端实际上会使用不同的语言。

我应该使用来自java.net.*Socket api,而不是使用什么?如果我使用 Socket API,我可以使用 c++ 和 php 与该服务器通信吗?还是我应该使用 REST?

任何方向表示赞赏。

【问题讨论】:

  • REST 真的只在 HTTP 上下文中才有意义。您绝对可以使用直接在原始套接字上运行的自写 API 来使用 REST 背后的原理,但那时它还不是 REST。
  • 我希望能够通过 HTTP 进行通信。但不确定要使用哪个 API。可能在自定义端口上。

标签: java sockets rest


【解决方案1】:

您可以使用很多东西来创建休息服务,然后它们几乎可以被任何东西使用。特别令人敬畏的是能够在您的网络浏览器中点击它们,因为我们都非常熟悉它们。

当我需要一个“快速而肮脏”的休息解决方案时,我会使用Restlet - 我不会声称它是唯一的解决方案,但它是我使用过的最简单的解决方案。我在一次会议上确实说过“我可以在 10 分钟内让 XYZ 起来”。会议中的其他人打电话给我,果然,使用 Restlet,我能够获得一个运行正常的 REST 服务器,并使用我说我会在会议中获得的(非常简单的)功能。这很漂亮。

这是一个准系统服务器,它有一个方法,返回当前时间。运行服务器并点击127.0.0.1:12345/sample/time 将返回当前时间。

import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Context;
import org.restlet.Restlet;
import org.restlet.data.Protocol;
import org.restlet.routing.Router;

/**
 * This Application creates an HTTP server with a singple service
 * that tells you the current time.
 * @author corsiKa
 */
public class ServerMain extends Application {

    /**
     * The main method. If you don't know what a main method does, you 
     * probably are not advanced enough for the rest of this tutorial.
     * @param args Command line args, completely ignored.
     * @throws Exception when something goes wrong. Yes I'm being lazy here.
     */
    public static void main(String...args) throws Exception {
        // create the interface to the outside world
        final Component component = new Component();
        // tell the interface to listen to http:12345
        component.getServers().add(Protocol.HTTP, 12345);
        // create the application, giving it the component's context
        // technically, its child context, which is a protected version of its context
        ServerMain server = new ServerMain(component.getContext().createChildContext());
        // attach the application to the interface
        component.getDefaultHost().attach(server);
        // go to town
        component.start();

    }

    // just your everyday chaining constructor
    public ServerMain(Context context) {
        super(context);
    }

    /** add hooks to your services - this will get called by the component when
     * it attaches the application to the component (I think... or somewhere in there
     * it magically gets called... or something...)
     */
    public Restlet createRoot() {
        // create a router to route the incoming queries
        Router router = new Router(getContext().createChildContext());
        // attach your resource here
        router.attach("/sample/time", CurrentTimeResource.class);
        // return the router.
        return router;
    }

}

这是它使用的“当前时间资源”:

import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

/**
 * A resource that responds to a get request and returns a StringRepresentaiton
 * of the current time in milliseconds from Epoch
 * @author corsiKa
 */
public class CurrentTimeResource extends ServerResource {

    @Get // add the get annotation so it knows this is for gets
    // method is pretty self explanatory
    public Representation getTime() {
        long now = System.currentTimeMillis(); 
        String nowstr = String.valueOf(now); 
        Representation result = new StringRepresentation(nowstr);
        return result;
    }
}

【讨论】:

  • 你需要了解一些东西,我有兴趣创建一个服务器,就像 HTTP 服务器一样,使用 JAVA,自定义,将通过 REST 调用监听传入连接。我可以使用 Restlet 吗?
  • 是的,你可以用 Restlet 做到这一点。
  • +1 您好,您能否提供一个示例链接,用于为 RESTful 服务创建 Restlet http 服务器?
  • @NitinJS 我在我的答案中添加了一个准系统服务器实现。这使用了最新版本的 Restlet。玩得开心!
【解决方案2】:

JAX-RS 是 Java 中的 REST API。实现有很多,但主要有 Jersey(参考实现)、Apache 的 CXF 和 Restlet。

【讨论】:

    【解决方案3】:

    通常,您不会创建服务器。您创建一个 Web 应用程序并将其部署在服务器或 servlet 容器上。一些 servlet 容器可以嵌入到您的 Web 应用程序中,例如码头。流行的免费 servlet 容器有 Tomcat、Glassfish、Jetty 等。

    对于 Restlet,它不是一个 servlet 容器。它是一个允许您创建具有 RESTful 样式的 wep 应用程序的框架。所以这不应该混淆。

    如果您决定使用 servlet 容器,那么问题在于如何创建具有 RESTful 样式的 Web 应用程序。 REST 不是一种技术——它是一种设计原则。这是一个关于如何创建界面的概念。

    REST 的设计是无状态的,因此您不需要存储事务的状态。请求中的所有信息都应足以产生对客户的响应。

    有几个 servlet 框架可以让您轻松实现 REST 样式。其中一些非常复杂,例如Spring 框架。

    【讨论】:

    • 但是你是如何实现一个带有 Socker Client Server peer 的 Spring Boot Rest Server 的呢?
    【解决方案4】:

    下载 JBoss 7。问题已解决。

    这是一项宁静的服务:

    @Path("/myservice")
    public class MyService {
    
        @GET
        @Produces(MediaTypes.TEXT_PLAIN)
        public String echoMessage(@QueryParam("msg") String msg) {
            return "Hello " + msg;
        }
    }
    

    创建一个 WAR。部署。打开浏览器并转到http://myserver/myapp/myservice?msg=World。完成!

    【讨论】:

    • 谁告诉你的? JBoss 6 存在严重的性能问题,但除此之外,该系列一直很稳定。 JBoss 7 是一个杀手。
    • 我需要一个罐子吗?我使用哪个 API 来做到这一点?
    • 我只能访问 jboss 4。我在一家公司工作。我不能使用 jboss 7 :(
    • JBoss 4?哇,太糟糕了,没想到还有人用它。您可以使用 Servlet 框架实现您自己的基于 REST 的服务,但这需要做更多的工作。
    【解决方案5】:

    我会说使用基于 HTTP 的 RESTful 服务。它正在迅速成为事实上的标准。查看我对此similar question 的回答,了解优缺点。

    【讨论】:

      【解决方案6】:

      这是最好的。 Spring MVC 本身具有很多 REST 功能。这只需要包含一个 jar 文件。你们都准备好使用它的所有功能了。甚至像下面这个人在 JBOSS 中解释的那样简单,而且还具有更大的灵活性。

      http://java.dzone.com/articles/spring-30-rest-example

      【讨论】:

        【解决方案7】:

        我将 JBoss 6 与 RestEasy 一起使用。我创建了一个位于here 的教程。希望对您有所帮助。

        【讨论】:

          【解决方案8】:

          也欢迎您查看我非常简单的 REST 服务器实现示例here

          【讨论】:

            猜你喜欢
            • 2020-02-07
            • 2011-02-12
            • 1970-01-01
            • 2014-02-22
            • 2013-11-04
            • 1970-01-01
            • 1970-01-01
            • 2011-04-01
            • 1970-01-01
            相关资源
            最近更新 更多