简体   繁体   English

com.fasterxml.jackson.core.JsonParseException:无法识别的令牌'Hello':期待(JSON字符串,数字,数组,)

[英]com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Hello': was expecting (JSON String, Number, Array,)

I am having problem sending React multipart form data to backend MySQL using Java.我在使用 Java 将 React 多部分表单数据发送到后端 MySQL 时遇到问题。 When I test for React and Java individually in Postman, it works fine.当我在 Postman 中分别测试 React 和 Java 时,它工作正常。 The problem occurs when I use the form to send the user data along with a picture from frontend to backend, I get Unrecognized token error.当我使用表单将用户数据以及图片从前端发送到后端时,会出现问题,我收到无法识别的令牌错误。 I have researched this error a lot and can't seem to find the solution.我已经对此错误进行了很多研究,但似乎找不到解决方案。 Please help where I am going wrong.请帮助我哪里出错了。

React:反应:

const [firstName, setFirstName] = useState()
const [lastName, setLastName] = useState()
const [email, setEmail] = useState()
const [phoneNumber, setPhoneNumber] = useState()
const [file, setFile] = useState()

const send = event => {
    const data = new FormData()
    data.append("user", firstName)
    data.append("file", file)


    Axios.post("/new/sale", data).then(res => console.log(res))
        .catch(err => console.log(err))

}

Controller Controller

 @PostMapping(value = "/sale")
    public ResponseEntity<Response> createPost(@RequestParam("file") MultipartFile file,
                                               @RequestParam("user") String user)
            throws IOException {

        ObjectMapper obj = new ObjectMapper();
        Sale sale = obj.readValue(user, Sale.class);
        sale.setPicture(file.getBytes());
        sale.setFileName(file.getOriginalFilename());

        Sale sales = saleRepository.save(sale);
        if(sales != null) {
            return new ResponseEntity<Response>(HttpStatus.OK);
        } else {
            return new ResponseEntity<Response>(HttpStatus.BAD_REQUEST);
        }
    }

POJO POJO

 @Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
private Date createdDate;
private Date updatedDate;
private byte [] picture;
private String fileName;

You have a problem with the header of the request send by axios.您对 axios 发送的请求的 header 有问题。 You have to edit your request to set header content-type to multipart/form-data :您必须编辑您的请求以将 header content-type设置为multipart/form-data

const config = {
    headers: {
        'content-type': 'multipart/form-data'
     }
 }

And add this config like 3rd paramter to axios POST request:并将此配置(如第三个参数)添加到 axios POST 请求:

Axios.post("/new/sale", data , config).then(res => console.log(res))
.catch(err => console.log(err))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 引起:com.fasterxml.jackson.core.JsonParseException:无法识别的令牌&#39;okhttp3&#39;:期待(JSON字符串&#39;) - Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'okhttp3': was expecting (JSON String') 由以下原因引起的错误:com.fasterxml.jackson.core.JsonParseException:无法识别的令牌“ Employee”:正在期待(“ true”,“ false”或“ null”) - Error Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Employee': was expecting ('true', 'false' or 'null') Spring Boot com.fasterxml.jackson.core.JsonParseException:无法识别的令牌 - Spring Boot com.fasterxml.jackson.core.JsonParseException: Unrecognized token 错误 com.fasterxml.jackson.core.JsonParseException:无法识别的令牌 - Error com.fasterxml.jackson.core.JsonParseException: Unrecognized token Spring Boot com.fasterxml.jackson.core.JsonParseException:无法识别的令牌“食谱” - Spring Boot com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Recipe' Apache Camel when deserializing an object, it throws an exception: com.fasterxml.jackson.core.JsonParseException: Unrecognized token - Apache Camel when deserializing an object, it throws an exception: com.fasterxml.jackson.core.JsonParseException: Unrecognized token com.fasterxml.jackson.core.JsonParseException是否为* .json.swp? - com.fasterxml.jackson.core.JsonParseException for *.json.swp? com.fasterxml.jackson.core.JsonParseException:无法识别的字符转义符&#39;U&#39;(代码85) - com.fasterxml.jackson.core.JsonParseException: Unrecognized character escape 'U' (code 85) 如何使无法识别的字符转义 &#39;.&#39; (代码 46)被识别 - com.fasterxml.jackson.core.JsonParseException - How to make Unrecognized character escape '.' (code 46) to be Recognized - com.fasterxml.jackson.core.JsonParseException 找不到com.fasterxml.jackson.core.JsonParseException的类文件 - class file for com.fasterxml.jackson.core.JsonParseException not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM