1

I am trying to send a multipart/formdata object to the backend that contains a Freelancer object and two image files that have to be stored on the server. The saving on disc part works, but saving the JSON string as a freelancer object is not working. I tried converting the string with Jackson objectmapper but I think I am doing something wrong. When I debug the application it crashes at mapper.readValue and goes straight to the catch().

I also tried to work with kotlinx.serializer, but the import just would not work so I switched to Jackson.

The Kotlin controller that takes in the request:

   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()
    }
}

The request from the front end using vue:

enter image description here

The console output of the formdata:

enter image description here

Freelancer model:

@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,

    )
mm da
  • 55
  • 7
  • [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) – gidds Jan 16 '21 at 22:50
  • What is the exception you're getting in `catch` ? – miensol Jan 17 '21 at 08:42
  • @miensol This is what e.message says: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token at [Source: (String)"{"workExperience":9,"firstName":"1","dateOfBirth":"2020-02-19"...whole object in image above...} line: 1, column: 351] (through reference chain: com.example.bouwershub.models.Freelancer["identificationImage"]) I think there is something wrong with binding it with my model? – mm da Jan 17 '21 at 23:00
  • Does this answer your question? [com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of \`java.util.ArrayList\` out of START\_OBJECT token](https://stackoverflow.com/questions/58539657/com-fasterxml-jackson-databind-exc-mismatchedinputexception-cannot-deserialize) – Martin Zeitler Jan 17 '21 at 23:35
  • So the problem was that my frontend was not sending a field for id, but my model does expect it. Now the mapper does word, but I created another problem where my model maps the userId to the freelancer id but this error shows up: attempted to assign id from null one-to-one property [model.freelancer.freelancerId] – mm da Jan 18 '21 at 11:27

1 Answers1

0

I solved it by adding ID to the Json object I was sending to the backend, because my model has a one-to-one relation with another model I had to include it when mapping the Json to the model.

mm da
  • 55
  • 7