0

I am trying to create a basic spring API. I have a Post class that have an attribute User user as foreign key.

package com.example.demo.model;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import jakarta.persistence.*;

import java.util.Objects;

@Entity
public class Post {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false, updatable = false)
    private Long id;
    private String title;
    private String body;
    @ManyToOne
    private User user;

    public Post() {
    }

    public Post(String title, String body) {
        this.title = title;
        this.body = body;
    }

   // Getters and Settes ...
}

Here is the User class

package com.example.demo.model;

import jakarta.persistence.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Entity
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false, updatable = false)
    private Long id;
    private String name;
    private Integer age;
    private String email;

    @OneToMany(mappedBy = "user")
    private List<Post> posts = new ArrayList<Post>();

    @ManyToMany
    @JoinTable(name = "user_task",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "task_id"))
    private List<Task> tasks = new ArrayList<Task>();


    public User() {}

    public User(String name, Integer age, String email) {
        this.name = name;
        this.age = age;
        this.email = email;
    }

   // Getters and Settes ...
}

and here is my Post Controller

package com.example.demo.controller;

import com.example.demo.model.Post;
import com.example.demo.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/post")
public class PostController {

    private final PostService postService;

    @Autowired
    public PostController(PostService postService) {
        this.postService = postService;
    }

    @GetMapping("/all")
    public List<Post> getAllPosts (){
        System.out.println("3");
        return postService.getAllPosts();
    }

    @GetMapping("/{id}")
    public Post getPost(@PathVariable Long id){
        System.out.println("2");
        return postService.getPost(id);
    }

    @PostMapping("/create")
    public Post createPost(@RequestBody Post post){
        return postService.createPost(post);
    }
}

So in the /create endpoint i am passing a json object in the body. Here is an exemple:

{
    "title": "Post1",
    "body": "Post1 Body",
    "user": "1"
}

the user: 1 is the foreign key to user who owns the post.

Here is the full error:

Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.example.demo.model.User` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('1')]

I need to insert the json object into the Post table with the foreign key

  • Does this answer your question? [Spring boot & JSON: How to add foreign key when using POST request](https://stackoverflow.com/questions/73345078/spring-boot-json-how-to-add-foreign-key-when-using-post-request) – deepakchethan Jan 17 '23 at 02:22
  • There are some similarities but because i am new to spring boot and jpa i couldn't get it to work on my code – abdoessordo Jan 17 '23 at 15:22

0 Answers0