휴식 서비스를 만드는 데 사용할 수있는 많은 것들이 있습니다. 그런 다음 거의 모든 것이 소비 될 수 있습니다. 특정 awesomeness의 능력은 귀하의 웹 브라우저에서 그들을 공격하는 것입니다, 왜냐하면 우리 모두가 그들에게 익숙하기 때문입니다.
'빠르고 더러운'휴식 솔루션이 필요한 경우 Restlet을 사용합니다. 유일한 해결책이라고 주장하지는 않겠지 만 지금까지 사용해 본 것이 가장 쉽습니다. 저는 회의에서 "나는 XYZ를 10 분 안에 올릴 수있었습니다."라고 말했습니다. 회의에서 다른 사람이 저에게 전화를 걸었습니다. 물론 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;
}
}
[REST] (http://en.wikipedia.org/wiki/REST)는 HTTP의 컨텍스트에서 실제로 만 의미가 있습니다. 원시 소켓을 통해 직접 실행되는 자체 작성 API를 사용하여 REST 뒤에 _principles_을 확실히 사용할 수 있지만 그 시점에서는 REST가 아닐 수 있습니다. – sarnold
잘 HTTP를 통해 통신 할 수 싶습니다. 어떤 API를 사용할 지 잘 모르겠습니다. 가능하면 사용자 지정 포트에서. – DarthVader