RESTFUL路径

来源:互联网 发布:arm处理器系列 知乎 编辑:程序博客网 时间:2024/06/05 05:58
method 
 get,put,post,delete,head


@GET
@Path("{id}")
public String getCustomer(@PathParam("id") int id) {
...
}




URL:example:
@Path("customers/{firstname}-{lastname}")
@Path("{id : \\d+}") //regular expressions




请求路径为,中间有附加的东西 /cars/mercedes/e55;color=black/2006
@Path("/cars/{make}")
public class CarResource {
@GET
@Path("/{model}/{year}")
@Produces("image/jpeg")
public Jpeg getPicture(@PathParam("make") String make,
@PathParam("model") PathSegment car,   //简介调用 片段
@PathParam("year") String year) {
String carColor = car.getMatrixParameters().getFirst("color");


@GET
@Path("/{model}/{year}")
@Produces("image/jpeg")
public Jpeg getPicture(@PathParam("make") String make,
@PathParam("model") String model,
@MatrixParam("color") String color) { //直接调用片段
...
}








public class CarResource {
@GET
@Path("/{model}/{year}")
@Produces("image/jpeg")
public Jpeg getPicture(@Context UriInfo info) { //直接注入context 查看api进行相关调用
String make = info.getPathParameters().getFirst("make");
PathSegment model = info.getPathSegments().get(1);
String color = model.getMatrixParameters().getFirst("color");
...
}
}




查询
GET /customers?start=0&size=10
public String getCustomers(@QueryParam("start") int start,
@QueryParam("size") int size) {


查询
GET /customers?start=0&size=10
@Path("/customers")
public class CustomerResource {
@GET
@Produces("application/xml")
public String getCustomers(@Context UriInfo info) {
String start = info.getQueryParameters().getFirst("start");
String size = info.getQueryParameters().getFirst("size"); //上下文获取查询
...
}
}




表单接收
@Path("/customers")
public class CustomerResource {
@POST
public void createCustomer(@FormParam("firstname") String first,
@FormParam("lastname") String last) {
...
}
}



原创粉丝点击