繁体   English   中英

How to post an image placed inside a json object in reactjs to a spring boot back end rest api

[英]How to post an image placed inside a json object in reactjs to a spring boot back end rest api

我正在创建一个简单的应用程序,它在前端获取用户详细信息,并将它们发送到后端。 该应用程序的前端使用 ReactJs 开发,而后端完全是 spring 引导。 在这里,您可以看到我在前端生成的 object,然后将其发送到后端。

    this.state = {
        user_details:{
            first_name: '',
            last_name: '',
            phone_number: '',
            email: '',
            password: '',
            birth_date: new Date(),
            nic: '',
            street_address: '',
            street_address2: '',
            city: '',
            state:'',
            zip_code: '',
            nationality: '',
            religion: '',
            photo: null
        }

正如您在上面看到的,我正在从用户那里获取一个图像文件,然后将该图像分配给 this.state.user_details.photo,它最初是 null。

当用户单击 reactJs 表单中的提交按钮时,user_details object 将使用以下 axios 调用发送到后端,

saveUserDetails(user_details){
    return axios.post("http://localhost:8080/teacher_details",user_details)
}

此请求将由后端定义的以下 PostMapping 方法处理

@PostMapping("/teacher_details")
public void saveTeacherDetails(@RequestBody UserDetails userDetails) {
    userRepo.save(userDetails);
    
}

The UserDetails class is the wrapper class which responsible to map the values in the request body to an object, here is the UserDetails class's member variables,

private String email;
private String nic;
private String firstName;
private String lastName;
private String phoneNumber;
private Date birthDate;
private String streetAddr1;
private String streedAddr2;
private String state;
private String city;
private String zipCode;
private String nationality;
private String religion;
private MultipartFile photo;

我确实有所有的 getter、setter、没有 args 构造函数和所有 args 构造函数但是当我单击前端的提交按钮时,我得到以下异常,

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.web.multipart.MultipartFile` (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information

我不知道这里发生了什么。 我想要的只是将图像和数据一起添加到 UserDetails 的 ArrayList 中。

我在这里做错了什么,或者这里的错误是什么。 谁能解释我

将您的 json 更改为 formdata,下面的 function 将有所帮助

 createFormData(object, form, namespace) {
        const formData = form || new FormData();
        for (let property in object) {
            if (!object.hasOwnProperty(property) ||      !object[property]) {
                continue;
            }
            const formKey = namespace ? `${namespace}[${property}]` : property;
            if (object[property] instanceof Date) {
                formData.append(formKey, object[property].toISOString());
            } else if (typeof object[property] === 'object' && !(object[property] instanceof File)) {
                createFormData(object[property], formData, formKey);
            } else {
                formData.append(formKey, object[property]);
            }
        }
        return formData;
    }

并删除 controller 中的@RequestBody ,因为我们现在不发布 json 而是表单数据;

现在修改这个方法

saveUserDetails(user_details){
return axios.post("http://localhost:8080/teacher_details",user_details)
}

saveUserDetails(user_details){
let formData=createFormData(user_details);
return axios.post("http://localhost:8080/teacher_details",formData)
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM