将 JSON 字符串设为 Kotlin 对象不起作用
Making JSON string to Kotlin object not working
我正在尝试向后端发送一个 multipart/formdata 对象,其中包含一个 Freelancer 对象和两个必须存储在服务器上的图像文件。保存在光盘部分有效,但将 JSON 字符串保存为自由职业者对象无效。我尝试使用 Jackson objectmapper 转换字符串,但我认为我做错了什么。当我调试应用程序时,它在 mapper.readValue
崩溃并直接进入 catch()
.
我也尝试使用 kotlinx.serializer
,但导入不起作用,所以我切换到 Jackson。
接受请求的 Kotlin 控制器:
private val imageDirectory = System.getProperty("user.dir") + "/images/"
@PostMapping(consumes = ["multipart/form-data"])
fun saveUser(
@RequestParam("profileImage") profileImage: MultipartFile,
@RequestParam("fileIdImage") fileIdImage: MultipartFile,
@RequestParam("freelancer") freelancer: String,
): ResponseEntity<*> {
return try {
val mapper = ObjectMapper();
val freelancerJson: Freelancer = mapper.readValue(freelancer, Freelancer::class.java)
println(freelancerJson.aboutYou)
makeDirectoryIfNotExist(imageDirectory)
val profileImagePath: Path = Paths.get(imageDirectory, profileImage.originalFilename)
val idImagePath: Path = Paths.get(imageDirectory, fileIdImage.originalFilename)
Files.write(profileImagePath, profileImage.bytes);
Files.write(idImagePath, fileIdImage.bytes);
JsonResponse(HttpStatus.OK, "Saved freelancer} ").createResponseEntity()
} catch (e: Exception) {
JsonResponse(HttpStatus.INTERNAL_SERVER_ERROR, e.message.toString()).createResponseEntity()
}
}
前端使用vue的请求:
表单数据的控制台输出:
自由职业者模型:
@Entity
data class Freelancer(
@Id
val id: Int,
//maps ID to freelancer primary key
@MapsId
@OneToOne(targetEntity = User::class)
@JoinColumn(name = "freelancer_id")
//ignores the freelancer id because it is already mapped to val id
@JsonIgnore
val freelancerId: User,
val firstName: String? = null,
val lastName: String? = null,
val dateOfBirth: Date? = null,
val kvk: String? = null,
val btw: String? = null,
val phone: String? = null,
val street: String? = null,
val zipcode: String? = null,
val city: String? = null,
val housenumber: Int? = 0,
val addition: String? = null,
val nameCardHolder: String? = null,
val iban: String? = null,
val referral: String? = null,
val specialism: String? = null,
val aboutYou: String? = null,
val motivation: String? = null,
val workExperience: Int? = null,
val driverLicense: Boolean? = null,
val ownTransport: Boolean? = null,
val identificationImage: String? = null,
val smallBusinessScheme: Boolean? = false,
val profileImage: String? = null,
val previousWorkedPlatform: String? = null,
val servicesAmount: Int? = null,
val previousWorkedRating: Int? = null,
val foundBy: String? = null,
val privacyStatement: Boolean? = null,
)
我通过向发送到后端的 Json 对象添加 ID 解决了这个问题,因为我的模型与另一个模型有一对一的关系,所以我在映射 [= 时必须包含它12=] 到模型。
我正在尝试向后端发送一个 multipart/formdata 对象,其中包含一个 Freelancer 对象和两个必须存储在服务器上的图像文件。保存在光盘部分有效,但将 JSON 字符串保存为自由职业者对象无效。我尝试使用 Jackson objectmapper 转换字符串,但我认为我做错了什么。当我调试应用程序时,它在 mapper.readValue
崩溃并直接进入 catch()
.
我也尝试使用 kotlinx.serializer
,但导入不起作用,所以我切换到 Jackson。
接受请求的 Kotlin 控制器:
private val imageDirectory = System.getProperty("user.dir") + "/images/"
@PostMapping(consumes = ["multipart/form-data"])
fun saveUser(
@RequestParam("profileImage") profileImage: MultipartFile,
@RequestParam("fileIdImage") fileIdImage: MultipartFile,
@RequestParam("freelancer") freelancer: String,
): ResponseEntity<*> {
return try {
val mapper = ObjectMapper();
val freelancerJson: Freelancer = mapper.readValue(freelancer, Freelancer::class.java)
println(freelancerJson.aboutYou)
makeDirectoryIfNotExist(imageDirectory)
val profileImagePath: Path = Paths.get(imageDirectory, profileImage.originalFilename)
val idImagePath: Path = Paths.get(imageDirectory, fileIdImage.originalFilename)
Files.write(profileImagePath, profileImage.bytes);
Files.write(idImagePath, fileIdImage.bytes);
JsonResponse(HttpStatus.OK, "Saved freelancer} ").createResponseEntity()
} catch (e: Exception) {
JsonResponse(HttpStatus.INTERNAL_SERVER_ERROR, e.message.toString()).createResponseEntity()
}
}
前端使用vue的请求:
表单数据的控制台输出:
自由职业者模型:
@Entity
data class Freelancer(
@Id
val id: Int,
//maps ID to freelancer primary key
@MapsId
@OneToOne(targetEntity = User::class)
@JoinColumn(name = "freelancer_id")
//ignores the freelancer id because it is already mapped to val id
@JsonIgnore
val freelancerId: User,
val firstName: String? = null,
val lastName: String? = null,
val dateOfBirth: Date? = null,
val kvk: String? = null,
val btw: String? = null,
val phone: String? = null,
val street: String? = null,
val zipcode: String? = null,
val city: String? = null,
val housenumber: Int? = 0,
val addition: String? = null,
val nameCardHolder: String? = null,
val iban: String? = null,
val referral: String? = null,
val specialism: String? = null,
val aboutYou: String? = null,
val motivation: String? = null,
val workExperience: Int? = null,
val driverLicense: Boolean? = null,
val ownTransport: Boolean? = null,
val identificationImage: String? = null,
val smallBusinessScheme: Boolean? = false,
val profileImage: String? = null,
val previousWorkedPlatform: String? = null,
val servicesAmount: Int? = null,
val previousWorkedRating: Int? = null,
val foundBy: String? = null,
val privacyStatement: Boolean? = null,
)
我通过向发送到后端的 Json 对象添加 ID 解决了这个问题,因为我的模型与另一个模型有一对一的关系,所以我在映射 [= 时必须包含它12=] 到模型。