欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  类库下载

jersey + tomcat 实现restful风格

程序员文章站 2022-03-21 19:05:55
...
环境:

idea 15.0.2

jersey 1.3

tomcat 7.0

maven 3.3.3

1.idea 基于maven 构建webapp 略过

2.项目构建完成之后pom.xml 文件加入项目所需包:

junitjunit4.7testcom.sun.jerseyjersey-core1.3com.sun.jerseyjersey-server1.3com.sun.jerseyjersey-client1.3log4jlog4j1.2.14javax.ws.rsjsr311-api1.1.1asmasm3.2

3.创建pojo类 Student:

@XmlRootElement
public class Student {
    private int id;
    private String name;
    private String dept;

    public int getId() {
        return id;
    }

    public Student() {
    }

    public Student(int id, String name, String dept) {
        super();
        this.id = id;
        this.name = name;
        this.dept = dept;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDept() {
        return dept;
    }
    public void setDept(String dept) {
        this.dept = dept;
    }

}

同时创建资源类:

@Path("/students")
public class RestWsDemo {
    private static Logger logger = Logger.getLogger(RestWsDemo.class);
    private static int index = 1;
    private static Map studentList = new HashMap();

    public RestWsDemo() {
        if(studentList.size()==0) {
            studentList.put(index, new Student(index++, "Frank",  "CS"));
            studentList.put(index, new Student(index++, "Jersey", "Math"));
        }
    }

    @GET
    @Path("{studentid}")
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Student getMetadata(@PathParam("studentid") int studentid) {
        if(studentList.containsKey(studentid))
            return studentList.get(studentid);
        else
            return new Student(0, "Nil", "Nil");
    }

    @GET
    @Path("list")
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public List getAllStudents() {
        List students = new ArrayList();
        students.addAll(studentList.values());
        return students;
    }

    @POST
    @Path("add")
    @Produces("text/plain")
    public String addStudent(@FormParam("name") String name,
                             @FormParam("dept") String dept) {
        studentList.put(index, new Student(index++, name, dept));
        return String.valueOf(index-1);
    }

    @DELETE
    @Path("delete/{studentid}")
    @Produces("text/plain")
    public String removeStudent(@PathParam("studentid") int studentid) {
        logger.info("Receieving quest for deleting student: " + studentid);

        Student removed = studentList.remove(studentid);
        if(removed==null) return "failed!";
        else   return "true";
    }

    @PUT
    @Path("put")
    @Produces("text/plain")
    public String putStudent(@QueryParam("studentid") int studentid,
                             @QueryParam("name") String name,
                             @QueryParam("dept") String dept
    ) {
        logger.info("Receieving quest for putting student: " + studentid);
        if(!studentList.containsKey(studentid))
            return "failed!";
        else
            studentList.put(studentid, new Student(studentid, name, dept));

        return String.valueOf(studentid);
    }
}

创建完之后项目结构如图:

jersey + tomcat 实现restful风格

4.再web.xml 配置如下:

jerseywscom.sun.jersey.spi.container.servlet.ServletContainercom.sun.jersey.config.property.resourceConfigClasscom.sun.jersey.api.core.PackagesResourceConfigcom.sun.jersey.config.property.packagescz.service1jerseyws/rest/*

其中 com.sun.jersey.config.property.packages 的属性值是你资源所在的包的路径

5.maven install 略过

6.启动tomcat 访问路径 http://localhost:8081/rest/students/list 就看以看到如下结果:

jersey + tomcat 实现restful风格

7.其他资源获取方式自行领悟、测试