申利民
骑士
骑士
  • UID582
  • 粉丝0
  • 关注0
  • 发帖数11
阅读:4537回复:0

request.getParameter(key),你奏凯!

楼主#
更多 发布于:2021-07-12 05:22
可以这样获取参数:
Object value = request.getParameter(key);
参数很多的话。就无限个这样的语句霸屏。


实际上可以一次性获取
public static Map<String, Object> getParameters(HttpServletRequest request) {
   Map<String, Object> map = new HashMap<>();
  Enumeration<String> enumeration = request.getParameterNames();
  while (enumeration.hasMoreElements()) {
        String key = (String) enumeration.nextElement();
        Object value = request.getParameter(key);
        if (!isEmpty(value)) {
                 map.put(key, value);
        }
}
   return map;
}





实际项目中这样使用的:



@GET
@POST
@Path("/quality/region/count.do")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(notes = "数据状态[区域站] - 24小时情况统计 ")
@ApiParams({@ApiParam(name = "token", note = "token", required = true),
        @ApiParam(name = "area_code", note = "区划Code", required = true, value = "1000000111"),
        @ApiParam(name = "search_time", note = "查询时间", value = "2021-04-08", required = true),
        @ApiParam(name = "station_code", note = "站号", value = "", required = false),
        @ApiParam(name = "station_level", note = "站级别", value = "", required = false),
        @ApiParam(name = "station_type", value = "03", note = "站类型", required = true),
        @ApiParam(name = "status", note = "数据状态 1(缺测),2(缺报)", value = "1", required = false)})
public ResponseData countQualityRegion() {

Map<String, Object> parMap = Utils.getParameters(request);
DBSession session = DataBaseUtil.getHibernateSession(DATA_CENTER);
try {
    ...................
} catch (Exception e) {
   e.printStackTrace();
} finally {
  closeObject(session);
}

return FALSE();
}
游客

返回顶部