RESTEasy:JAX-RS restful webservices 示例

纯web版本

pom添加依赖:


        
            org.jboss.resteasy
            resteasy-jaxrs
            3.0.11.Final
        
    
web.xml添加servlet定义


    resteasy-servlet
    org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
    
      javax.ws.rs.Application
      test.MyRESTEasyApplication
    
  
  
    resteasy-servlet
    /*
  

JAX-RS定义

package test;

import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;

public class MyRESTEasyApplication extends Application {
    private Set singletons = new HashSet();

    public MyRESTEasyApplication() { singletons.add(new HelloWorldRestService()); }

    @Override
    public Set getSingletons() { return singletons; }
}
 
  
package test;

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

@Path("/hello")
public class HelloWorldRestService {
    @GET
    public Response defaultResponse() {
        return Response.status(404).entity("404: Default Response ....").build();
    }

    @GET
    @Path("/{param}")
    public Response getName(@PathParam("param") String name) {
        String result = "RESTEasy Hello World : " + name;
        return Response.status(200).entity(result).build();
    }
}

web Guice集成版本

pom添加依赖:

        
            org.jboss.resteasy
            resteasy-guice
            3.0.16.Final
        
    
web.xml添加内容:
    
        resteasy.guice.modules
        guice.hello.MyGuiceModule
    

    
        
            org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener
        
    

    
        Resteasy
        org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
    

    
        Resteasy
        /*
    

java代码:
package guice.hello;

public interface Greeter {
    String greet(String name);
}
package guice.hello;

import javax.inject.Singleton;

@Singleton
public class DefaultGreeter implements Greeter {
    public String greet(String name) {
        System.out.println(this);
        return "Hello " + name;
    }
}
package guice.hello;

import com.google.inject.Binder;
import com.google.inject.Module;

// TODO: 可参考 core 模块中的例子:com.conquer.comutils.core.guice.MyGuiceModule
public class MyGuiceModule implements Module {
    public void configure(final Binder binder) {
        binder.bind(MyResource.class);
        binder.bind(Greeter.class).to(DefaultGreeter.class)
        //                使用 javax.inject.Singleton() 标注 实现类 实现单例
//                .in(com.google.inject.Scopes.SINGLETON)// 这里设置单例,默认不是单例的而是每次创建
        ;
    }
}
package guice.hello;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

@Path("hello")
public class MyResource {
    private final Greeter greeter;

    @Inject
    public MyResource(final Greeter greeter) {
        this.greeter = greeter;
    }

    @GET
    @Path("{name}")
    public String hello(@PathParam("name") final String name) {
        return greeter.greet(name);
    }
}

javase版本

RESTEasy可以通过javase程序main方法直接运行服务,生产环境下一般不这么使用,SpringBoot也有类似的用法,不多说了。






你可能感兴趣的:(其它)