top.wboost.common.base.restful.AutoRequestMethodInvoke Maven / Gradle / Ivy
package top.wboost.common.base.restful;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.http.HttpServletRequest;
import org.springframework.data.domain.Page;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import top.wboost.common.annotation.Explain;
import top.wboost.common.base.annotation.AutoWebApplicationConfig;
import top.wboost.common.base.entity.ResultEntity;
import top.wboost.common.base.page.QueryPage;
import top.wboost.common.system.code.SystemCode;
import top.wboost.common.utils.web.utils.ConvertUtil;
import top.wboost.common.utils.web.utils.HibernateUtil;
@AutoWebApplicationConfig("autoRequestMethodInvoke")
public class AutoRequestMethodInvoke {
private PathMatcher pathMatcher = new AntPathMatcher();
/** path : (requestMethod:value)**/
private Map> autoMehotdMap = new ConcurrentHashMap<>();
public void addAutoRequestMethod(String path, AutoRequestMethod method) {
if (this.autoMehotdMap.get(path) == null) {
this.autoMehotdMap.put(path, new ConcurrentHashMap<>());
}
this.autoMehotdMap.get(path).put(method.getRequestMapping().method()[0].toString(), method);
}
public Map> getAutoRequestMethod() {
return this.autoMehotdMap;
}
private AutoRequestMethod getRequestMethod(HttpServletRequest request) {
String url = request.getRequestURI().substring(request.getContextPath().length(),
request.getRequestURI().length());
Iterator>> iterator = this.autoMehotdMap.entrySet().iterator();
while (iterator.hasNext()) {
Entry> entry = iterator.next();
if (pathMatcher.match(entry.getKey(), url)) {
return entry.getValue().get(request.getMethod().toUpperCase());
}
}
return null;
}
/**
* 快速根据id查询
* @param id 查询id
* @param request
* @return
*/
@ResponseBody
@Explain(value = "根据id查询数据", systemCode = SystemCode.QUERY_FAIL)
@RequestMapping(value = "/{inspectName}/{id}", method = RequestMethod.GET)
public ResultEntity QUERY_BY_ID(@PathVariable("id") String id, HttpServletRequest request) {
AutoRequestMethod method = getRequestMethod(request);
Object obj = method.getService().findById(id);
HibernateUtil.loadLazy(obj);
return ResultEntity.success(SystemCode.QUERY_OK).setData(obj).build();
}
public String[] constantArray = new String[] {};
/**
* 快速根据参数查询列表
* @param map 参数
* @param request
* @return
*/
@ResponseBody
@Explain(value = "列表查询", systemCode = SystemCode.QUERY_FAIL)
@RequestMapping(value = "/{inspectName}", method = RequestMethod.GET)
public ResultEntity QUERY_LIST(@RequestParam Map map, HttpServletRequest request, QueryPage page) {
AutoRequestMethod method = getRequestMethod(request);
Object converterObj = ConvertUtil.mapConvertToBean(map, method.getEntityClass());
Page