package com.rest.resource;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
import com.rest.pojo.Customer;
import com.rest.service.CustomerService;
@Path("customers")
@Produces("application/json;charset=UTF-8")
@Consumes("application/json;charset=UTF-8")
public class Customers {
private CustomerService customerService = new CustomerService();
public CustomerService getCustomerService() {
return customerService;
}
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
@GET
public List<Customer> getCustomers() {
System.out.println("getCustomers called");
List<Customer> customers = (List<Customer>) getCustomerService().getCustomers();
if (customers == null) {
System.out.println("getCustomers null");
ResponseBuilder builder = Response.status(Status.BAD_REQUEST);
builder.type("application/json");
builder.entity("<error>Customers Not Found</error>");
throw new WebApplicationException(builder.build());
} else {
System.out.println("getCustomers");
return customers;
}
}
@GET
@Path("/{id}")
public Customer getCustomerById(@PathParam("id") String id) {
System.out.println("getCustomerById called with customer's id: " + id);
Customer customer = (Customer) getCustomerService().getCustomerById(id);
if (customer == null) {
ResponseBuilder builder = Response.status(Status.NOT_FOUND);
builder.type("application/json");
builder.entity("<error>Customer Not Found</error>");
throw new WebApplicationException(builder.build());
} else {
System.out.println("getCustomer");
return customer;
}
}
@POST
public Customer insertCustomer(Customer customer){
System.out.println("insertCustomer called");
Customer cust = (Customer) getCustomerService().getCustomerById(customer.getId());
if (cust != null) {
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).build());
} else {
getCustomerService().insertCustomer(customer);
return customer;
}
}
@DELETE
@Path("/{id}")
public Customer deleteCustomer(@PathParam("id") String id){
System.out.println("deleteCustomer with customer's id : " + id);
Customer customer = (Customer) getCustomerService().getCustomerById(id);
if (customer == null) {
throw new WebApplicationException(Response.status(Status.NOT_FOUND).build());
} else {
getCustomerService().deleteCustomer(id);
return customer;
}
}
@PUT
@Path("/{id}")
public Customer updateCustomer(@PathParam("id") String id, Customer customer) {
System.out.println("updateStudent with customer's id : "+ id);
Customer cust = (Customer) getCustomerService().getCustomerById(id);
if (cust == null) {
throw new WebApplicationException(Response.status(Status.NOT_FOUND).build());
} else {
getCustomerService().updateCustomer(customer);
return customer;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
RESTDemo.zip (33个子文件)
RESTDemo
WebContent
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 1KB
lib
mysql-connector-java-5.1.7-bin.jar 693KB
asm-3.1.jar 42KB
jersey-server-1.18.jar 599KB
jersey-servlet-1.18.jar 124KB
jettison-1.1.jar 66KB
jackson-core-asl-1.9.2.jar 223KB
jackson-jaxrs-1.9.2.jar 18KB
jersey-json-1.18.jar 161KB
jackson-xc-1.9.2.jar 26KB
jersey-client-1.18.jar 131KB
jersey-core-1.18.jar 468KB
jsr311-api-1.1.1.jar 45KB
jackson-mapper-asl-1.9.2.jar 748KB
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 305B
org.eclipse.wst.common.component 476B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 503B
org.eclipse.jdt.core.prefs 364B
src
com
rest
dao
CustomerDAO.java 3KB
filter
CorsFilter.java 2KB
pojo
Customer.java 836B
resource
Customers.java 3KB
service
CustomerService.java 2KB
.project 1KB
.classpath 2KB
build
classes
com
rest
dao
CustomerDAO.class 4KB
filter
CorsFilter.class 2KB
pojo
Customer.class 1KB
resource
Customers.class 4KB
service
CustomerService.class 3KB
共 33 条
- 1
资源评论
lenin13
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功