使用javax.ws.rs实现Restlet开发REST方式的webservice

使用javax.ws.rs实现Restlet开发REST方式的webservice

JSR311(javax.ws.rs)是Java实现REST Web Service 的规范标准。
本文阐述使用Restlet(http://www.restlet.org)进行REST web service开发步骤。


1)下载restlet最新的稳定版restlet-jee-2.0.3
2)将 javax.ws.rs.jar
        javax.xml.bind.jar
        org.json.jar
        org.restlet.ext.jaxrs.jar
        org.restlet.ext.json.jar
        org.restlet.ext.servlet.jar
        org.restlet.jar

放在WEB-INF/lib目录下

注意,网上有些文章提到需要
com.noelios.restlet.jar
com.noelios.restlet.ext.servlet_2.5.jar
这两个包的是针对restlet旧版(1.1)的jar包,在2版本的实现中已经全部集中到org.restlet包中了

3)使用javax.ws.rs的注解和jaxb的注解编写返回xml数据的请求

package app.test;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement

public class Person {


        private String name;

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }
       
}

 

package app.test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
@Path("test")
public class ExampleResource {
            @Path("person/xml")
            @GET
            @Produces("application/xml")
            public Person createPerson() {
                    Person xx = new Person();
                    xx.setName("Helloworld");
                    return xx;
            }
            @GET
            @Path("person/json")
            @Produces("application/json")
            public Person createPerson1() {
                    Person xx = new Person();
                    xx.setName("Helloworld");
                    return xx;
            }
           
            @GET
            @Path("person/{id}")
            public String findPerson(@PathParam("id") String id){
                    Person xx = new Person();
                    xx.setName(id);
                    return xx.getName();
            }

}

 

package app.test;

import java.util.HashSet;
import java.util.Set;
public class ExampleApplication extends javax.ws.rs.core.Application {

    public Set> getClasses() {
        Set> rrcs = new HashSet>();
        rrcs.add(ExampleResource.class);
        return rrcs;
    }
}

 

package app.test;

import org.restlet.Context;
import org.restlet.ext.jaxrs.JaxRsApplication;

public class JaxRsExtensionApplication extends JaxRsApplication {

    public JaxRsExtensionApplication(Context context) {
        super(context);

        this.add(new ExampleApplication());
       
    }
}

 

5)编写web.xml


        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

       
        org.restlet.application
        app.test.JaxRsExtensionApplication
       

       


RestletServlet
org.restlet.ext.servlet.ServerServlet


RestletServlet
/*

 

6)访问测试

http://127.0.0.1:8080/test/person/xml
返回xml数据
http://127.0.0.1:8080/test/person/json
返回json数据
http://127.0.0.1:8080/test/person/11
返回plainText

7)使用AJAX方式请求就不多赘述了
8)参考资料
http://www.ibm.com/developerwork ... &S_CMP=reg-ccid

你可能感兴趣的:(Web,Service)