搭建rest服务

依赖的jar包:
javax.ws.rs.jar
org.restlet.jar
org.restlet.ext.jaxrs.jar
 
RestletServer.java
import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;

import org.restlet.Component;
import org.restlet.Context;
import org.restlet.data.Protocol;
import org.restlet.ext.jaxrs.JaxRsApplication;


public class RestletServer 
{
    public static void main(String[] args)
    {
        Component component = new Component();
        // 启动HTTP接口
        component.getServers().add(Protocol.HTTP, 8080);
        component.getClients().add(Protocol.HTTP);
        Context context = component.getContext().createChildContext();
        context.getParameters().add("socketTimeout", "300000");
        context.getParameters().add("stopIdleTimeout", "300000");
        context.getParameters().add("idleTimeout", "300000");

        final JaxRsApplication application = new JaxRsApplication(context);
        application.add(new OpenServerApplication());
        component.getDefaultHost().attach(application);

        try
        {
            component.start();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

class OpenServerApplication extends Application
{
    @Override
    public Set<Class<?>> getClasses()
    {
        final Set<Class<?>> rrcs = new HashSet<Class<?>>();
        rrcs.add(RestResourceImpl.class);
        return rrcs;
    }
}

 

IRestResource.java

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("exceptionClean")
public interface IRestResource
{
    @GET
    @Path("clean/{num}/")
    public Response cleanCrashByProduct(@PathParam("num") int num);
}

 

RestResourceImpl.java

import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;


public class RestResourceImpl implements IRestResource
{
    @Override
    public Response cleanCrashByProduct(@PathParam("num") int num)
    {
        System.out.println("yeah!!!");
        return Response.status(Response.Status.OK).build();
    }
}

 

本地访问地址:

http://localhost:8080/exceptionClean/clean/111

 

 

 

posted @ 2015-01-26 20:23  iyjhabc  阅读(256)  评论(0编辑  收藏  举报