MyEclipse支持Rest服务,开发起来非常方便,下面我就举一个计算机的例子:
实现功能:加、减、乘、除;
效果如下:
![](http://my.csdn.net/uploads/201205/21/1337579949_9757.PNG)
![](http://my.csdn.net/uploads/201205/21/1337579951_9624.PNG)
![](http://my.csdn.net/uploads/201205/21/1337579954_2257.PNG)
![](http://my.csdn.net/uploads/201205/21/1337579956_6681.PNG)
效果如下:
Rest服务要点:每个服务或任何东西都有一个URI;
![](http://my.csdn.net/uploads/201205/21/1337579816_2571.png)
- package org.xiazdong.service;
- import javax.ws.rs.GET;
- import javax.ws.rs.Path;
- import javax.ws.rs.PathParam;
- import javax.ws.rs.Produces;
- import com.sun.jersey.spi.resource.Singleton;
- @Produces("text/plain")
- @Path("calculator")
- @Singleton
- public class MyService {
- @GET //设置此服务为get请求的服务
- @Path("add/{id}") //FirstRest/calculator/add/1+1
- public String add(@PathParam("id") String s){
- String[]ss = s.split("\+");
- return s+"="+String.valueOf(Integer.parseInt(ss[0])+Integer.parseInt(ss[1]));
- }
- @GET
- @Path("sub/{id}") //FirstRest/calculator/add/1-1
- public String sub(@PathParam("id") String s){
- String[]ss = s.split("\-");
- return s+"="+String.valueOf(Integer.parseInt(ss[0])-Integer.parseInt(ss[1]));
- }
- @GET
- @Path("mult/{id}") //FirstRest/calculator/add/1*1
- public String mult(@PathParam("id") String s){
- String[]ss = s.split("\*");
- return s+"="+String.valueOf(Integer.parseInt(ss[0])*Integer.parseInt(ss[1]));
- }
- @GET
- @Path("div/{id}") //FirstRest/calculator/add/1~1
- public String div(@PathParam("id") String s){
- String[]ss = s.split("~");
- return s+"="+String.valueOf(Integer.parseInt(ss[0])/Integer.parseInt(ss[1]));
- }
- }
- @POST //设置此服务为post请求的服务
- @Path("add")
- public String add(@FormParam("tt") String s){ //tt为表单的textfield的name
- String[]ss = s.split("\+");
- return s+"="+String.valueOf(Integer.parseInt(ss[0])+Integer.parseInt(ss[1]));
- }