Restlet with Jetty Configuration

时间:2018-12-19 11:15:16

标签: web-services jetty restlet

Dependencies:

RestLet 2.3
Jetty 9.4

MyServer.Java

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.restlet.Component;
import org.restlet.Server;
import org.restlet.data.Protocol;

public class MyServer {
    public static void main(String[] a) {
        Component component = new Component();
        Server server = component.getServers().add(Protocol.HTTP, 5000);
        server.getContext().getParameters().add("useForwardedForHeader", "true");
        component.getDefaultHost().attach(new MyApplication());

        try {
            System.out.println("Going to start MyServer");
            component.start();
            System.out.println("Server started on port " + port);
        } catch (Exception e) {
            log.error(e, e);
        }
    }
}

MyApplication.java

import com.myApp.resources.*;
import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;

public class MyApplication extends Application {
    @Override
    public Restlet createInboundRoot() {
        Router router = new Router(getContext());
        router.attach("/App", MyResource.class);
        return router;
    }
}

MyResource.Java

import org.restlet.resource.ServerResource;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Date;

@Path("/getDetails")
public class MyResource extends ServerResource{

    @GET
    @Path("/{number}")
    public Object getData(@PathParam("number") String number) {
        try {
            System.out.println("number: " + number);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Response.ResponseBuilder builder = Response.ok("Received", MediaType.TEXT_PLAIN);
        return builder.build();
    }

Now I'm starting my application it starting with the following console log...

Going to start MyServer
[main] INFO org.eclipse.jetty.util.log - Logging initialized @291ms to org.eclipse.jetty.util.log.Slf4jLog
[main] WARN org.eclipse.jetty.server.AbstractConnector - Ignoring deprecated socket close linger time
Starting the Jetty [HTTP/1.1] server on port 5000
[main] INFO org.eclipse.jetty.server.Server - jetty-9.4.14.v20181114; built: 2018-11-14T21:20:31.478Z; git: c4550056e785fb5665914545889f21dc136ad9e6; jvm 1.8.0_181-b13
[main] INFO org.eclipse.jetty.server.AbstractConnector - Started ServerConnector@3327bd23{HTTP/1.1,[http/1.1]}{0.0.0.0:5001}
[main] INFO org.eclipse.jetty.server.Server - Started @446ms
Starting com.myApp.server.MyApplication application
Server started on port 5000

Now when I'm trying to get the result by using localhost:5000/App/getDetails/5426. I'm unable to get the response. Even I'm getting following message on the console...

[qtp586617651-20] WARN org.eclipse.jetty.server.HttpChannel - /App/getDetails/GJ0125 java.io.IOException: Restlet exception

Any help would be appreciated. Also if any other way to implement then let me know.

0 个答案:

没有答案