rest接口
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.QueryParam; import com.zyn.ssh.pojo.Teacher; import com.zyn.ssh.service.rest.base.ListData; import com.zyn.ssh.service.rest.base.PageData; import com.zyn.ssh.service.rest.base.SimpleData; import com.zyn.ssh.service.rest.base.SingleData; @Path("") public interface TeacherResource { @Path("/") @GET public ListData<Teacher> listTeachers(); @Path("/{pageNo}/{pageSize}") @GET public PageData<Teacher> listTeachersByPage(@PathParam("pageNo")int pageNo,@PathParam("pageSize")int pageSize); @Path("/") @POST public SingleData<Teacher> addTeacher(@QueryParam("name") String name); @Path("/{id}") @DELETE public SimpleData<String> deleteTeacher(@PathParam("id") int id); @Path("/{id}") @PUT public SingleData<Teacher> updateTeacher(@PathParam("id")int id,@QueryParam("name") String name); @Path("/{id}") @GET public SingleData<Teacher> getTeacher(@PathParam("id") int id); }
rest接口实现类
public class TeacherResourceImpl implements TeacherResource { @Resource(name = "teacherService") private TeacherService teacherService; public ListData<Teacher> listTeachers() { ListData<Teacher> listData = new ListData<Teacher>(); try { RetdObj<List<Teacher>> ret = this.teacherService.getAll(); listData.setData(ret.getRealObj()); System.out.println(ret.getRealObj().size()); listData.setCode(RespCode.valueOf(ret.getCode())); } catch (Exception e) { listData.setCode(RespCode.EX_APP); e.printStackTrace(); } return listData; } public PageData<Teacher> listTeachersByPage(int pageNo, int pageSize) { PageData<Teacher> pageData = new PageData<Teacher>(); Page<Teacher> page = new Page<Teacher>(); page.setPageNo(pageNo); page.setPageSize(pageSize); try { RetdObj<Page<Teacher>> ret = this.teacherService .findAllByPage(page); pageData.setData(ret.getRealObj().getResult()); pageData.setPageinfo(CommonUtils.setPageInfoFromPage(ret .getRealObj())); pageData.setCode(RespCode.valueOf(ret.getCode())); } catch (Exception e) { pageData.setCode(RespCode.EX_APP); e.printStackTrace(); } return pageData; } public SingleData<Teacher> addTeacher(String name) { SingleData<Teacher> singleData = new SingleData<Teacher>(); if ("".equals(name)) { singleData.setCode(RespCode.EX_PARAM); return singleData; } Teacher teacher = new Teacher(name); try { this.teacherService.save(teacher); singleData.setCode(RespCode.PASS_OK); singleData.setData(teacher); } catch (Exception e) { e.printStackTrace(); singleData.setCode(RespCode.EX_APP); } return singleData; } public SimpleData<String> deleteTeacher(int id) { SimpleData<String> simpleData = new SimpleData<String>(); try { this.teacherService.delete(id); simpleData.setCode(RespCode.PASS_OK); } catch (Exception e) { e.printStackTrace(); simpleData.setCode(RespCode.EX_APP); } return simpleData; } public SingleData<Teacher> updateTeacher(int id, String name) { SingleData<Teacher> singleData = new SingleData<Teacher>(); if ("".equals(name)) { singleData.setCode(RespCode.EX_PARAM); return singleData; } try { Teacher teacher = this.teacherService.findById(id).getRealObj(); if (teacher == null) { singleData.setCode(RespCode.PASS_NODATA); } else { teacher.setName(name); this.teacherService.update(teacher); singleData.setCode(RespCode.PASS_OK); singleData.setData(teacher); } } catch (Exception e) { e.printStackTrace(); singleData.setCode(RespCode.EX_APP); } return singleData; } public SingleData<Teacher> getTeacher(int id) { SingleData<Teacher> singleData = new SingleData<Teacher>(); try{ RetdObj<Teacher> ret = this.teacherService.findById(id); singleData.setCode(RespCode.valueOf(ret.getCode())); singleData.setData(ret.getRealObj()); }catch(Exception e){ singleData.setCode(RespCode.EX_APP); e.printStackTrace(); } return singleData; } }
返回简单对象,基本类型包装类:
@XmlRootElement(name = "base") public class BaseRespObj implements Serializable { private static final long serialVersionUID = -3197616622643404121L; @XmlElement(name = "infocode") private RespCode respCode; public BaseRespObj() { this.setCode(RespCode.NODEFINE); } public void setCode(RespCode retdCodeType) { this.respCode = retdCodeType; } public int getCode() { return respCode.getCode(); } }
@XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class SimpleData<T> extends BaseRespObj{ @XmlElement private T result; public T getResult() { return result; } public void setResult(T result) { this.result = result; } }
返回单一实体对象包装类
@SuppressWarnings("serial") @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement @XmlSeeAlso(value={Teacher.class}) public class SingleData<RealObject> extends BaseRespObj{ @XmlElement private RealObject data; public RealObject getData() { return data; } public void setData(RealObject data) { this.data = data; } }
返回list集合包装类
@SuppressWarnings("serial") @XmlAccessorType(XmlAccessType.FIELD) @XmlSeeAlso(value={Teacher.class}) @XmlRootElement public class ListData<T> extends BaseRespObj{ @XmlElement private List<T> data; public List<T> getData() { return data; } public void setData(List<T> data) { this.data = data; } }
返回page分页数据包装类
@XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) @XmlType(propOrder = { "totalResults", "currentPage", "onePageSize", "firstResult", "nextPage", "previousPage", "totalPage" }) public class PageRespObj implements Serializable { private static final long serialVersionUID = -2940983877096774934L; private int currentPage; // 当前页 private int firstResult; // 当前页的起始记录 private long totalResults; // 总共记录数 private int onePageSize; // 每页的数量 private long totalPage; // 总共多少页 private int nextPage; // 下一页 private int previousPage; // 上一页 public PageRespObj() { } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getFirstResult() { return firstResult; } public void setFirstResult(int firstResult) { this.firstResult = firstResult; } public long getTotalResults() { return totalResults; } public void setTotalResults(long totalResults) { this.totalResults = totalResults; } public int getOnePageSize() { return onePageSize; } public void setOnePageSize(int onePageSize) { this.onePageSize = onePageSize; } public long getTotalPage() { return totalPage; } public void setTotalPage(long totalPage) { this.totalPage = totalPage; } public int getNextPage() { return nextPage; } public void setNextPage(int nextPage) { this.nextPage = nextPage; } public int getPreviousPage() { return previousPage; } public void setPreviousPage(int previousPage) { this.previousPage = previousPage; } }
@XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement @XmlSeeAlso(value={Teacher.class}) public class PageData<T> extends BaseRespObj{ @XmlElement private PageRespObj pageinfo; @XmlElementWrapper(name="pageData") public List<T> data; public PageRespObj getPageinfo() { return pageinfo; } public void setPageinfo(PageRespObj pageinfo) { this.pageinfo = pageinfo; } public List<T> getData() { return data; } public void setData(List<T> data) { this.data = data; } }
返回的系统状态码
@XmlRootElement @XmlEnum public enum RespCode { @XmlEnumValue("0")NODEFINE(0), @XmlEnumValue("200")PASS_OK(200), @XmlEnumValue("201")PASS_NODATA(201), @XmlEnumValue("500")EX_PARAM(500), @XmlEnumValue("501")EX_APP(501), @XmlEnumValue("502")EX_AUTH(502), @XmlEnumValue("600")EX_UNKNOWN(600); private int code; RespCode(int code) { this.code = code; } public int getCode() { return this.code; } public static RespCode valueOf(int code) { switch(code) { case 0 : return NODEFINE; case 200 : return PASS_OK; case 201 : return PASS_NODATA; case 500 : return EX_PARAM; case 501 : return EX_APP; case 502 : return EX_AUTH; case 600 : return EX_UNKNOWN; default : return null; } } }
将service层返回的page数据封装成rest返回类型帮助类
public class CommonUtils { /** * * @param page * @return */ @SuppressWarnings("rawtypes") public static PageRespObj setPageInfoFromPage(Page page) { int currentPage = 0; int onePageSize = 0; long totalResults = 0; int firstResult = 0; int nextPage = 0; int previousPage = 0; long totalPage = 0; if (page != null) { currentPage = page.getPageNo(); onePageSize = page.getPageSize(); totalResults = page.getTotalCount(); firstResult = page.getFirst(); nextPage = page.getNextPage(); previousPage = page.getPrePage(); totalPage = page.getTotalPages(); PageRespObj obj = new PageRespObj(); obj.setCurrentPage(currentPage); obj.setOnePageSize(onePageSize); obj.setTotalResults(totalResults); obj.setFirstResult(firstResult); obj.setNextPage(nextPage); obj.setPreviousPage(previousPage); obj.setTotalPage(totalPage); return obj; } else { return null; } } }
配置cxf
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation=" http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <cxf:bus> <cxf:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" /> </cxf:inInterceptors> </cxf:bus> <jaxrs:server id="teacherRestResource" address="/teachers"> <jaxrs:extensionMappings> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> </jaxrs:extensionMappings> <jaxrs:serviceBeans> <bean class="com.zyn.ssh.service.rest.impl.TeacherResourceImpl" /> </jaxrs:serviceBeans> </jaxrs:server> </beans>