用rest创建一个java服务器 [英] Creating a java server with rest

查看:97
本文介绍了用rest创建一个java服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to create a rest server and client.

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

I stumbled upon this tutorial which uses sockets. I want to be able to use REST calls, possibly HTTP, because the clients will be actually in different languages.

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

Instead of using Socket api from java.net.* what should i use ? if i use Socket API can I use c++ and php to communicate with this server? or should i go with REST?

任何指示赞赏。

推荐答案

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

There's a lot of things you can use to create rest services, and then they can be consumed by almost anything. Of particular awesomeness is the ability to hit them in your web browser, just because we're all so familiar with them.

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

When I need a 'quick and dirty' rest solution, I use Restlet - I won't claim it's the only solution, but it's the easiest I've ever worked with. I've literally said in a meeting "I could have XYZ up in 10 minutes." Someone else in the meeting called me on it, and sure enough, using Restlet I was able to get a functioning REST server running with the (very simple) features I said I would get in the meeting. It was pretty slick.

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

Here's a barebones server that has one method, returning the current time. Running the server and hitting 127.0.0.1:12345/sample/time will return the current 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;
    }

}

这是'当前时间资源'它使用:

And here's the 'current time resource' that it uses:

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;
    }
}

这篇关于用rest创建一个java服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆