jersey tomcat MySQL_jersey + tomcat 实现restful风格

本文参考 http://www.cnblogs.com/bluesfeng/archive/2010/10/28/1863816.html

环境:

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:

@XmlRootElementpublic classStudent {private intid;privateString name;privateString dept;public intgetId() {returnid;

}publicStudent() {

}public Student(intid, String name, String dept) {super();this.id =id;this.name =name;this.dept =dept;

}public void setId(intid) {this.id =id;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}publicString getDept() {returndept;

}public voidsetDept(String dept) {this.dept =dept;

}

}

同时创建资源类:

@Path("/students")public classRestWsDemo {private static Logger logger = Logger.getLogger(RestWsDemo.class);private static int index = 1;private static Map studentList = new HashMap();publicRestWsDemo() {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") intstudentid) {if(studentList.containsKey(studentid))returnstudentList.get(studentid);else

return new Student(0, "Nil", "Nil");

}

@GET

@Path("list")

@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})public ListgetAllStudents() {

List students = new ArrayList();

students.addAll(studentList.values());returnstudents;

}

@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") intstudentid) {

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") intstudentid,

@QueryParam("name") String name,

@QueryParam("dept") String dept

) {

logger.info("Receieving quest for putting student: " +studentid);if(!studentList.containsKey(studentid))return "failed!";elsestudentList.put(studentid,newStudent(studentid, name, dept));returnString.valueOf(studentid);

}

}

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

deb375fbfd7e75e0b9406d386c88a2e9.png

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 就看以看到如下结果:

9afdb02da00787bd75889a53fc70760e.png

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

第一篇处女博文,觉得不好请将就下看这,如果该博文侵犯 原作者版权请尽快联系我

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值