Reactjs 在使用@RequestParam从UI向后端调用API时,无法使spring引导接受有限的参数

Reactjs 在使用@RequestParam从UI向后端调用API时,无法使spring引导接受有限的参数,reactjs,spring-boot,Reactjs,Spring Boot,大家好,我正在使用React JS作为UI 首先假设我已经设计了表单,从中我得到了14个以下的值 file isSameMail Student_firstName Student_lastName Student_Email Student_details1 Student_details2 Student_details3 Student_details4 Student_details5 Student_details6 Student_details7 Student_details8

大家好,我正在使用React JS作为UI

首先假设我已经设计了表单,从中我得到了14个以下的值

file
isSameMail
Student_firstName
Student_lastName
Student_Email
Student_details1
Student_details2
Student_details3
Student_details4
Student_details5
Student_details6
Student_details7
Student_details8
Student_details9
我在Springboot API(registerStudent)中传递上述表单值,使用@RequestParam注册学生从UI调用API,如

 let data = new FormData();
                data.append('file', this.state.file);
                data.append('isSameMail', false);
                data.append('Student_firstName', fields.firstName);
                data.append('Student_lastName', fields.lastName);
                data.append('Student_Email', fields.email);
                data.append('Student_details1', fields.details1);
                data.append('Student_details2', fields.details2);
                data.append('Student_details3', fields.details3);
                data.append('Student_details4', fields.details4);
                data.append('Student_details5', fields.details5);
                data.append('Student_details6', fields.details6);
                data.append('Student_details7', fields.details7);
                data.append('Student_details8', fields.details8);
                data.append('Student_details9', fields.details9);
                const url = http://localhost:8084/student/registerStudent';

                fetch(url, {
                  method: 'POST',
                  body: data,
                })
                  .then(res => {
                    if (res.ok) {
                      return res.json();
                    }
                    throw new Error(res.status);
                  })
                  .then(res => {
                    })
我使用SpringBoot调用API,我从表单UI中获取上述所有值并将其存储在数据库(MYSQL)中

假设我正在使用API调用控制器

@CrossOrigin
 @PostMapping(path = "/student/registerStudent", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<Object> registerStudent(@RequestParam(value = "file", required = false) MultipartFile UploadRegistrationForm,
                                                  @RequestParam("isSameMail") boolean isSameMail, @RequestParam("Student_firstName") String firstName,
                                                  @RequestParam("Student_lastName") String lastName, @RequestParam("Student_Email") String email,
                                                  @RequestParam("Student_details1") String details1, @RequestParam("Student_details2") String details2,
                                                  @RequestParam(value = "Student_details3", required = false) Double details3,
                                                  @RequestParam(value = "Student_details4", required = false) Double details4,
                                                  @RequestParam(value = "Student_details5", required = false) Double details5,
                                                  @RequestParam(value = "Student_details6", required = false) Double details6,
                                                  @RequestParam(value = "Student_details7", required = false) Date details7,
                                                  @RequestParam(value = "Student_details8", required = false) Integer details8,
                                                  @RequestParam(value = "Student_details9", required = false) String details9) throws IOException {
}
@CrossOrigin
@PostMapping(path=“/student/registerStudent”,consumes=MediaType.MULTIPART\u FORM\u DATA\u VALUE)
public ResponseEntity registerStudent(@RequestParam(value=“file”,required=false)MultipartFile UploadRegistrationForm,
@RequestParam(“isSameMail”)布尔值isSameMail,@RequestParam(“Student_firstName”)字符串firstName,
@RequestParam(“Student_lastName”)字符串lastName、@RequestParam(“Student_Email”)字符串Email、,
@RequestParam(“学生详细信息1”)字符串详细信息1、@RequestParam(“学生详细信息2”)字符串详细信息2、,
@RequestParam(value=“Student\u details3”,required=false)双细节s3,
@RequestParam(value=“Student\u details4”,required=false)双细节4,
@RequestParam(value=“Student_details5”,required=false)双细节5,
@RequestParam(value=“Student_details6”,required=false)双细节6,
@RequestParam(value=“Student_details7”,required=false)日期详情7,
@RequestParam(value=“Student\u details8”,required=false)整数details8,
@RequestParam(value=“Student\u details9”,required=false)字符串细节9)引发IOException{
}
现在您可以看到,registerStudent()方法中有许多参数,但我不希望参数超过7个参数,除了多次定义RequestParam之外,使用@RequestParam的合适方法是什么


注意:我们得到1个多部分文件

如果我理解正确,这可能是您的解决方案

您可以创建一个对象,该对象将包含所有字段,并且它将在控制器方法中看起来更大

大概是这样的:

package com.example;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.Date;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class RegisterStudentReqestedParams {
    private MultipartFile UploadRegistrationForm;
    @NotNull
    private boolean isSameMail;
    @NotNull
    private String firstName;
    @NotNull
    private String lastName;
    @NotNull
    private String email;
    @NotNull
    private String details1;
    @NotNull
    private String details2;
    private Double details3;
    private Double details4;
    private Double details5;
    private Double details6;
    private Date details7;
    private Integer details8;
    private String details9;
}
@CrossOrigin
    @PostMapping(path = "/student/registerStudent", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<Object> registerStudent(@RequestBody @Valid RegisterStudentReqestedParams registerStudentReqestedParams){}
控制器现在看起来像这样:

package com.example;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.Date;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class RegisterStudentReqestedParams {
    private MultipartFile UploadRegistrationForm;
    @NotNull
    private boolean isSameMail;
    @NotNull
    private String firstName;
    @NotNull
    private String lastName;
    @NotNull
    private String email;
    @NotNull
    private String details1;
    @NotNull
    private String details2;
    private Double details3;
    private Double details4;
    private Double details5;
    private Double details6;
    private Date details7;
    private Integer details8;
    private String details9;
}
@CrossOrigin
    @PostMapping(path = "/student/registerStudent", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<Object> registerStudent(@RequestBody @Valid RegisterStudentReqestedParams registerStudentReqestedParams){}
@CrossOrigin
@PostMapping(path=“/student/registerStudent”,consumes=MediaType.MULTIPART\u FORM\u DATA\u VALUE)
public ResponseEntity registerStudent(@RequestBody@Valid RegisterStudentReqestedParams RegisterStudentReqestedParams){}

如果我理解正确,这可能是您的解决方案

您可以创建一个对象,该对象将包含所有字段,并且它将在控制器方法中看起来更大

大概是这样的:

package com.example;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.Date;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class RegisterStudentReqestedParams {
    private MultipartFile UploadRegistrationForm;
    @NotNull
    private boolean isSameMail;
    @NotNull
    private String firstName;
    @NotNull
    private String lastName;
    @NotNull
    private String email;
    @NotNull
    private String details1;
    @NotNull
    private String details2;
    private Double details3;
    private Double details4;
    private Double details5;
    private Double details6;
    private Date details7;
    private Integer details8;
    private String details9;
}
@CrossOrigin
    @PostMapping(path = "/student/registerStudent", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<Object> registerStudent(@RequestBody @Valid RegisterStudentReqestedParams registerStudentReqestedParams){}
控制器现在看起来像这样:

package com.example;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.Date;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class RegisterStudentReqestedParams {
    private MultipartFile UploadRegistrationForm;
    @NotNull
    private boolean isSameMail;
    @NotNull
    private String firstName;
    @NotNull
    private String lastName;
    @NotNull
    private String email;
    @NotNull
    private String details1;
    @NotNull
    private String details2;
    private Double details3;
    private Double details4;
    private Double details5;
    private Double details6;
    private Date details7;
    private Integer details8;
    private String details9;
}
@CrossOrigin
    @PostMapping(path = "/student/registerStudent", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<Object> registerStudent(@RequestBody @Valid RegisterStudentReqestedParams registerStudentReqestedParams){}
@CrossOrigin
@PostMapping(path=“/student/registerStudent”,consumes=MediaType.MULTIPART\u FORM\u DATA\u VALUE)
public ResponseEntity registerStudent(@RequestBody@Valid RegisterStudentReqestedParams RegisterStudentReqestedParams){}

Hey hi,通过上述方法,它给了我一个错误::已解决[org.springframework.web.HttpMediaTypeNotSupportedException:Content type'multipart/form data;boundary=----WebKitFormBoundaryhRjitDH2YBTAO0DA;charset=UTF-8'不受支持]@ShraddhaAgarwal您可以共享控制器的完整代码吗?您可以尝试使用
@modeldattribute
而不是
@RequestBody
嘿,嗨,按照上述方法,它会给我一个错误::已解决[org.springframework.web.HttpMediaTypeNotSupportedException:内容类型'multipart/form data;boundary=----WebKitFormBoundaryhRjitDH2YBTAO0DA;charset=UTF-8'不受支持]@ShraddhaAgarwal您可以共享控制器的完整代码吗?您可以使用
@modeldattribute
而不是
@RequestBody