欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

controller接收前端参数的几种方式

程序员文章站 2022-03-01 20:49:21
...

controller接收前端参数的几种方式

1.直接以对象的形式接收:

@RequestMapping(value = "/getTicketList", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
@ApiOperation(value = "获取工单列表", notes = "获取工单列表")
@ResponseBody
public CommonResultBO getTicketList(@RequestBody TicketInfoVO ticketInfoVO) {
    return ticketInfoService.selectTicketInfo(ticketInfoVO);
}
public CommonResultBO selectTicketInfo(TicketInfoVO ticketInfoVO) { // 得到对象后直接解析get值即可
    log.info("selectTicketInfo begin.");
    // 获取参数
    String ticketName = ticketInfoVO.getTicketName();
    List<String> ticketTypesList = ticketInfoVO.getTicketTypes();
    List<String> statusList = ticketInfoVO.getStatus();
    List<String> principalsList = ticketInfoVO.getPrincipals();
    List<String> prioritiesList = ticketInfoVO.getPriorities();
    String date = ticketInfoVO.getDate();
}

2.接收以@PequestParam注解修饰的参数,即跟在url后面的参数:/operationLog/getTicketById?id=18

@RequestMapping(value = "/getTicketById", method = RequestMethod.GET)
@ApiOperation(value = "根据id获取工单", notes = "根据id获取工单")
@ResponseBody
public CommonResultBO getTicketById(@RequestParam String id) { // 获取参数后直接传递即可
    return ticketInfoService.selectTicketById(id);
}

3.JSONObject形式:只能在POST请求方式下使用

@RequestMapping(value = "/processTicket", method = RequestMethod.POST)
@ApiOperation(value = "处理工单", notes = "工单处理")
@ResponseBody
public CommonResultBO processTicket(@RequestBody JSONObject request) { 
    log.info("processTicket begin.");
    String id = request.getString("id"); // getString()方法获取参数,也有其它getxxx()方法用来获取不同类型的参数。
    String reason = request.getString("reason");
    String solution = request.getString("solution");
    String processResult = request.getString("processResult");
    String remark = request.getString("remark");
    log.info("processTicket end.");
    return ticketInfoService.updateResultInfo(id, reason, solution, processResult, remark);
}

4.MultipartHttpServletRequest 多用于传递文件,和文件+参数混合的请求,十分方便。

@RequestMapping(value = "/editTicket", method = RequestMethod.POST)
@ApiOperation(value = "编辑工单", notes = "编辑工单")
@ResponseBody
public CommonResultBO editTicket(MultipartHttpServletRequest request) {
    log.info("editTicket begin.");
    try {
        TicketInfo ticketInfo = getTicketInfo(request);
        // 编辑信息
        return ticketInfoService.updateTicketInfo(ticketInfo);
    } catch (Exception exc) {
        exc.printStackTrace();
        log.error("editTicket error: " + exc.getMessage());
        return CommonResultBO.init(null, -1, "editTicket failed.");
    }
}
private TicketInfo getTicketInfo(MultipartHttpServletRequest request) {
    String ticketInfos = request.getParameter("ticketInfos"); // 根据参数名称获取参数 这里是获取了json字符串
    List<MultipartFile> files = request.getFiles("file");  // 根据参数名称获取参数 这里是获取了文件集合
    // 保存工单文件
    String path;
    path = this.readFile(files); // 读取MultipartFile类型文件
    // setFilePath
    TicketInfo ticketInfo = JSONObject.parseObject(ticketInfos, TicketInfo.class); // 得到json字符串后转换成实体类对象
    ticketInfo.setFilePath(path);
    return ticketInfo;
}
private String readFile(List<MultipartFile> files) { // 读取文件
    log.info("begin to read file.");
    List<String> paths = new ArrayList<>();
    for (MultipartFile file : files) {
        String path;
        try {
            // 获取文件名
            String fileName = file.getOriginalFilename();
            // 获取文件的后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            // 文件上传后的路径
            String filePath = "D:\\Download\\file\\";
            path = filePath + fileName;
            File dest = new File(path);
            // 检测是否存在目录
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            // 写入文件
            file.transferTo(dest);
        } catch (IOException exception) {
            exception.printStackTrace();
            log.error("createTicket error, file transfer error: " + exception.getMessage());
            return null;
        }
    }
    return String.join(",", path);
}

持续完善…

相关标签: java spring boot