用rest创建一个java服务器

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

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

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

任何方向赞赏。

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

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

这是一个有一个方法的准系统服务器,返回当前时间。 运行服务器并按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; } } 

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

通常,您不创建服务器。 您创建Web应用程序并将其部署在服务器或servlet容器上。 一些servlet容器可嵌入到您的Web应用程序中,例如Jetty。 Poppular免费servlet容器是Tomcat,Glassfish,Jetty等。

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

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

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

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

下载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 。 完成!

我会说基于HTTP的RESTful服务。 它正迅速成为事实上的标准。 检查我对这个类似问题的答案是否有利有弊。

这是最好的一个。 Spring MVC本身具有很多RESTfunction。 这只需要包含一个jar文件。 你们都准备好使用它的全部function。 即使像JBOSS中解释的那样简单,也有更多的灵活性。

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

我使用JBoss 6和RestEasy 。 我在这里创建了一个教程。 我希望这可以帮助你。

您也可以在这里看到我非常简单的REST服务器实现示例。