码迷,mamicode.com
首页 > 其他好文 > 详细

搭建rest服务

时间:2015-01-26 21:01:15      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

依赖的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

 

 

 

搭建rest服务

标签:

原文地址:http://www.cnblogs.com/iyjhabc/p/4251269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!