Creating a java server with relaxation

I need to create a server and client for the rest.

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

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

any valued directions.

+5
source share
8 answers

, , . -, , .

" " , Restlet - , , , - . : " XYZ 10 ". - , , , Restlet, REST, ( ) , , , . .

barebones, , . 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 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;
    }
}
+6

JAX-RS - API REST Java. , ( ), Apache CXF Restlet.

+2

. - . -, . Jetty. - Tomcat, Glassfish, Jetty ..

Restlet . , wep RESTful. .

, , - RESTful. REST - - . , .

REST , . , .

, REST. , . Spring.

+2

JBoss 7. .

:

@Path("/myservice")
public class MyService {

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

. . http://myserver/myapp/myservice?msg=World. !

+1

, RESTful HTTP. . .

0

. Spring MVC, REST. jar. . , JBOSS .

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

0

JBoss 6 RestEasy. , . , .

0

You can also see my very simple example for implementing a REST server here .

0
source

All Articles