记录 spring boot 无法获取 post json 数据

controller 层

package com.ruoyi.web.controller.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.biaoben.domain.FileInfo;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.config.ServerConfig;

/**
 * 通用请求处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/common")
public class CommonController
{
    private static final Logger log = LoggerFactory.getLogger(CommonController.class);

    @Autowired
    private ServerConfig serverConfig;

    private static final String FILE_DELIMETER = ",";
    /**
     * 通用图片删除请求(单个)
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody FileInfo js,
                             HttpServletRequest request,
                             @RequestParam(value = "filename",required = false) String fileName,
                             @RequestParam Map<String, Object> map
                             ){

        Map<String, Object> params = new HashMap<>();
        BufferedReader br = null;
        try {
            try {
                br = request.getReader();
            } catch (IOException e) {
                e.printStackTrace();
            }
            String str;
            StringBuilder wholeStr = new StringBuilder();
            while ((str = Objects.requireNonNull(br).readLine()) != null) {
                wholeStr.append(str);
            }
            if (StringUtils.isNotEmpty(wholeStr.toString())) {
                params = JSON.parseObject(wholeStr.toString(), Map.class);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(params);

        System.out.println(request.getMethod());
        System.out.println(map.get("fileName"));
        String result = getJSONParam(request).toJSONString();
        System.out.println(result);
//        System.out.println(fileName);
//        System.out.println("fileName:"+json.getString("fileName"));
        System.out.println(js.getFileName());
        System.out.println(js.toString());
        System.out.println(fileName);
//        try {
//            SecurityUtils.getLoginUser();
//        } catch (Exception e) {
//            throw new BaseException("没有删除权限!");
//        }
        //文件磁盘路径 /Users/wei/temp/traing/uploadPath
        String filePath = RuoYiConfig.getProfile();
        // 映射路径 /profile/upload/2023/02/21/QQ截图20230221113347_20230221113355A001.png
//        String fileName = fileInfo.getFileName();
        // /Users/wei/temp/traing/uploadPath/upload/2023/02/21/QQ截图20230221113347_20230221113355A001.png
        String deletaPath = filePath + StringUtils.substringAfter(fileName, Constants.RESOURCE_PREFIX);

        if (FileUtils.deleteFile(deletaPath)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    public JSONObject getJSONParam(HttpServletRequest request) {
        JSONObject jsonParam = null;
        try {
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = streamReader.readLine()) != null) {
                sb.append(line);
            }
            jsonParam = JSONObject.parseObject(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonParam;
    }

}

输出

{filename=/profile/upload/2023/06/05/aJiao_20230605173642A001.mp4}
POST
null
{"filename":"/profile/upload/2023/06/05/aJiao_20230605173642A001.mp4"}
null
FileInfo{filename='null'}
null

@RequestBody 等三种方式都无法获取道 ,其他接口同样使用实体类可以获取到数据

@RequestBody FileInfo js,
 HttpServletRequest request,
@RequestParam(value = "filename",required = false) String fileName,
@RequestParam Map<String, Object> map

实体类

package com.ruoyi.biaoben.domain;

import com.ruoyi.common.core.domain.BaseEntity;

public class FileInfo extends BaseEntity {

    String filename;

    public String getFileName() {
        return filename;
    }

    public void setFileName(String fileName) {
        this.filename = fileName;
    }

    @Override
    public String toString() {
        return "FileInfo{" +
                "filename='" + filename + '\'' +
                '}';
    }
}

记录 spring boot 无法获取 post json 数据_第1张图片
json 数据

{
    "filename": "/profile/upload/2023/06/05/aJiao_20230605173642A001.mp4"
}

你可能感兴趣的:(spring,spring,boot,json,java)