Java tutorial
/* * Copyright 2015-2016 http://hsweb.me * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.hsweb.web.controller; import com.alibaba.fastjson.JSON; import org.hsweb.commons.ClassUtils; import org.hsweb.web.bean.common.QueryParam; import org.hsweb.web.bean.po.GenericPo; import org.hsweb.web.core.authorize.annotation.Authorize; import org.hsweb.web.core.exception.BusinessException; import org.hsweb.web.core.exception.NotFoundException; import org.hsweb.web.core.logger.annotation.AccessLogger; import org.hsweb.web.core.message.ResponseMessage; import org.hsweb.web.service.GenericService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; import java.util.Arrays; /** * , * ???{@link GenericService}? */ public abstract class GenericController<PO, PK> { protected Logger logger = LoggerFactory.getLogger(this.getClass()); /** * ?Controller??,? * * @return ? */ protected abstract GenericService<PO, PK> getService(); /** * ?PO * * @return PO */ protected final Class<PO> getPOType() { return (Class<PO>) ClassUtils.getGenericType(this.getClass(), 0); } /** * ?PK() * * @return PK() */ protected final Class<PK> getPKType() { return (Class<PK>) ClassUtils.getGenericType(this.getClass(), 1); } /** * , * * @param param ? {@link QueryParam} * @return , ?()?:{total:?,data:[{}]}?. * ??[{}] */ @RequestMapping(method = RequestMethod.GET) @AccessLogger("") @Authorize(action = "R") public ResponseMessage list(QueryParam param) { // ?? Object data; if (!param.isPaging())//? data = getService().select(param); else data = getService().selectPager(param); return ResponseMessage.ok(data).include(getPOType(), param.getIncludes()) .exclude(getPOType(), param.getExcludes()).onlyData(); } /** * ?id? * * @param id * @return * @throws NotFoundException ??? */ @RequestMapping(value = "/{id}", method = RequestMethod.GET) @AccessLogger("") @Authorize(action = "R") public ResponseMessage info(@PathVariable("id") PK id) { PO po = getService().selectByPk(id); if (po == null) throw new NotFoundException("data is not found!"); return ResponseMessage.ok(po); } /** * ???? * * @param param ? * @return */ @RequestMapping(value = "/total", method = RequestMethod.GET) @AccessLogger("") @Authorize(action = "R") public ResponseMessage total(QueryParam param) { // ?? return ResponseMessage.ok(getService().total(param)); } /** * ?POST? * * @param object * @return ? * @throws javax.validation.ValidationException ??? */ @RequestMapping(method = RequestMethod.POST) @AccessLogger("") @Authorize(action = "C") @ResponseStatus(HttpStatus.CREATED) public ResponseMessage add(@RequestBody PO object) { PK pk = getService().insert(object); return ResponseMessage.created(pk); } /** * id??DELETErest /delete/1 id1? * * @param id ?id * @return * @throws NotFoundException ??? */ @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) @AccessLogger("") @Authorize(action = "D") public ResponseMessage delete(@PathVariable("id") PK id) { PO old = getService().selectByPk(id); assertFound(old, "data is not found!"); getService().delete(id); return ResponseMessage.ok(); } /** * ?? * * @param id ?? * @param object ?? * @return * @throws NotFoundException ??? */ @RequestMapping(value = "/{id}", method = RequestMethod.PUT) @AccessLogger("") @Authorize(action = "U") public ResponseMessage update(@PathVariable("id") PK id, @RequestBody PO object) { PO old = getService().selectByPk(id); assertFound(old, "data is not found!"); if (object instanceof GenericPo) { ((GenericPo) object).setId(id); } int number = getService().update(object); return ResponseMessage.ok(number); } /** * ??. * * @param json ? json? * @return ?? * @throws BusinessException ?? */ @RequestMapping(method = RequestMethod.PUT) @AccessLogger("?") @Authorize(action = "U") public ResponseMessage update(@RequestBody String json) { int number; if (json.startsWith("[")) { number = getService().update(JSON.parseArray(json, getPOType())); } else if (json.startsWith("{")) { number = getService().update(Arrays.asList(JSON.parseObject(json, getPOType()))); } else { throw new BusinessException("??!"); } return ResponseMessage.ok(number); } /** * ?, {@link NotFoundException} * * @param obj ? * @param msg null? */ public void assertFound(Object obj, String msg) { if (obj == null) throw new NotFoundException(msg); } }