首页>代码>Spring+Spring MVC+Mybatis+Jersey+Fastjson整合RESTful API框架,适合学习RESTful API框架初学者>/NICO-DEMO-Api/src/main/java/com/nico/service/AccountService.java
package com.nico.service; import java.util.List; import javax.transaction.Transactional; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.DefaultValue; 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.core.MediaType; import javax.ws.rs.core.Response; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.nico.dao.AccountMapper; import com.nico.entities.Account; import com.nico.entities.PagerEx; @Component @Path("/accounts") public class AccountService { @Autowired private AccountMapper accountMapper; @GET @Produces({"application/json; charset=UTF-8","application/xml; charset=UTF-8"}) public List<Account> getAccounts() { return accountMapper.getAccounts(); } @GET // @Path("/query") @QueryParam @Path("{pageSize}/{pageIndex}") // pageSize:每页记录数 pageIndex:当前页 @Produces({"application/json; charset=UTF-8","application/xml; charset=UTF-8"}) public List<Account> getAccountsByFilter(@DefaultValue("10") @PathParam("pageSize") int pageSize,@DefaultValue("1") @PathParam("pageIndex") int pageIndex) { int startIndex = (pageIndex-1)*pageSize; //开始l int endIndex = pageSize ; //条目数 PagerEx pagerEx = new PagerEx(); pagerEx.setPageIndex(startIndex); pagerEx.setPageSize(endIndex); return accountMapper.getAccountsByFilter(pagerEx); } @GET @Path("{accountID}") @Produces({"application/json; charset=UTF-8","application/xml; charset=UTF-8"}) public Response getAccountByAccountID(@PathParam("accountID") String accountID) { Account account = accountMapper.getAccountByAccountID(accountID); if(account != null) { return Response.status(200).entity(account).build(); } else { return Response.status(404).entity("The Account with the accountID " + accountID + " does not exist").build(); } } @DELETE @Path("{accountID}") @Produces({MediaType.TEXT_HTML}) @Transactional public Response deleteAccountByAccountID(@PathParam("accountID") String accountID) { if(accountMapper.deleteAccountByAccountID(accountID) == 1){ return Response.status(204).build(); } else { return Response.status(404).entity("Account with the AccountID " + accountID + " is not present in the database").build(); } } @POST @Path("batch") @Consumes({"application/json; charset=UTF-8"}) @Produces({MediaType.TEXT_HTML}) @Transactional public Response deleteAccountsBatch(List<String> list) { int num = accountMapper.deleteAccountsBatch(list); if(num > 0) { return Response.status(204).entity("Has deleted "+num+" Accounts").build(); }else { return Response.status(404).entity("Accounts deleted failed").build(); } } @POST @Consumes({"application/json; charset=UTF-8"}) @Produces({MediaType.TEXT_HTML}) @Transactional public Response createAccount(Account account) { int flag = accountMapper.createAccount(account); if(flag > 0){ return Response.status(201).entity("A new Account/Resource has been created").build(); }else { return Response.status(406).entity("A new Account/Resource create failed").build(); } } @PUT @Consumes({"application/json; charset=UTF-8"}) @Produces({MediaType.TEXT_HTML}) @Transactional public Response updateAccount(Account account) { String message; int status; if(accountWasUpdated(account)){ status = 200; //OK message = "Account has been updated"; } else { status = 406; //Not acceptable message = "The information you provided is not sufficient to perform either an UPDATE or " + " an INSERTION of the new hospital resource <br/>" + " If you want to UPDATE please make sure you provide an existent <strong>id</strong> <br/>" + " If you want to insert a new hospital please provide at least a <strong>title</strong> and the <strong>feed</strong> for the hospital resource"; } return Response.status(status).entity(message).build(); } private boolean accountWasUpdated(Account account) { return accountMapper.updateAccountByAccountID(account)== 1; } }
最近下载更多