Spring的controller接受Date类型数据,接受枚举类型数据

1. Controller接收Date类型的数据

核心使用@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") 来将传递过来的时间字符串转化为Date类型

@RestController
@RequestMapping("res")
public class BaseFlowController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(BaseFlowController.class);

    @Resource
    protected FamaOrderMapper orderMapper;

    @Resource
    private FamaUserMapper userMapper;

    @GetMapping("/query-all")
    @ApiException(QUERY_FLOW_FAILED)
    public Result query(@RequestParam(value = "beginTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date start,
                        @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date end,
                        @RequestParam(value = "processInstanceId", required = false) Integer processInstanceId,
                        @RequestParam(value = "state", required = false) ApproveStatus state,
                        @RequestParam(value = "applyType", required = false) ApplyType applyType) {
        Map<String, Object> data = Maps.newHashMap();
        try {
            LoginContext context = LoginContext.getLoginContext();
            String user = context == null ? "xxxxxx" : context.getPin();
            QueryWrapper<FamaUser> queryWrapper = Wrappers.query();
            queryWrapper.eq("erp",user);
            queryWrapper.eq("role_type", RoleType.SYS_ADMIN);
            FamaUser admin = userMapper.selectOne(queryWrapper);
            if (Optional.ofNullable(admin).isPresent()) {
                user = "";
            }
            List<FamaOrder> orders = orderMapper.selectByParam(user, start, end, applyType, processInstanceId, state);
            List<QueryResult> queryResults = ModuleHelper.mapAll(orders, QueryResult.class);
            queryResults.sort((o1, o2) -> o2.getCreateTime().compareTo(o1.getCreateTime()));
            data.put(Constants.DATA_LIST, queryResults);
            data.put(Constants.STATUS, SUCCESS);
            data.put(Constants.MSG, SUCCESS.getMsg());
        } catch (Exception e) {
            log.warn("create xbp order failed", e);
            data.put(Constants.STATUS, QUERY_FLOW_FAILED);
            data.put(Constants.MSG, QUERY_FLOW_FAILED.getMsg());
        }
        return returnDataList(data);
    }
}

2.Controller 接收枚举类型数据

EnvType env,直接进行映射,不需要再别的操作

@GetMapping(value = "/query-details-instance-types-cost-times")
    public Result queryDetailsInstanceTypesCostTimes(
            @RequestParam("env") EnvType env,
            @RequestParam("instanceId") String instanceId,
            @RequestParam("regionId") String regionId) {
        Map<String, Object> data;
        try {
            RdsContext context = RdsContext.builder().setEnvType(env).setRegionId(regionId).setInstanceId(instanceId).build();
            RdsRegionGetter getter = RdsRegionGetter.newInstance(context);
            data = getter.queryDetailsInstanceTypesCostTimes();
        } catch (Exception e) {
            logger.error("query-details-instance-types", e);
            data = Maps.newHashMap();
        }
        return returnDataList(data);
    }
posted @ 2021-06-01 10:24  SpecialSpeculator  阅读(591)  评论(0编辑  收藏  举报