您好,欢迎访问一九零五行业门户网

jersey + tomcat 实现restful风格

环境:
idea 15.0.2
jersey 1.3
tomcat 7.0
maven 3.3.3
1.idea 基于maven 构建webapp 略过
2.项目构建完成之后pom.xml 文件加入项目所需包:
junit junit 4.7 test com.sun.jersey jersey-core 1.3 com.sun.jersey jersey-server 1.3 com.sun.jersey jersey-client 1.3 log4j log4j 1.2.14 javax.ws.rs jsr311-api 1.1.1 asm asm 3.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); } }
创建完之后项目结构如图:
4.再web.xml 配置如下:
jerseyws com.sun.jersey.spi.container.servlet.servletcontainer com.sun.jersey.config.property.resourceconfigclass com.sun.jersey.api.core.packagesresourceconfig com.sun.jersey.config.property.packages cz.service 1 jerseyws /rest/*
其中 com.sun.jersey.config.property.packages 的属性值是你资源所在的包的路径
5.maven install 略过
6.启动tomcat 访问路径 http://localhost:8081/rest/students/list 就看以看到如下结果:
7.其他资源获取方式自行领悟、测试
其它类似信息

推荐信息