title: Spring boot insert json foreign key
body: <p>I have 2 tables called Country and State:</p>
  
  <p>Country:</p>
  
  <pre><code>@Entity
  public class Country implements Serializable {
  
  private static final long serialVersionUID = -7254786319390105939L;
  
  @Id
  @Column(name="id")
  private int id;
  
  @Column(name="name")
  private String name;
  
  @OneToMany(mappedBy = "country_id", targetEntity=State.class)
  @JsonBackReference
  private Set&lt;State&gt; states;
  }
  </code></pre>
  
  <p>State:</p>
  
  <pre><code>@Entity
  @Table(name = "state")
  public class State implements Serializable {
  
  private static final long serialVersionUID = -3569874119390105939L;
  
  @Id
  @Column(name = "id")
  private int id;
  
  @Column(name = "name")
  private String name;
  
  @ManyToOne(targetEntity=Country.class)
  @JoinColumn(name = "country_id",  referencedColumnName="id")
  @JsonManagedReference
  private Country country_id;
  }
  </code></pre>
  
  <p>The repositories and the controllers work correctly and with Postman i can get and post fine in the table Country, but went i try to Post in State an error appears:</p>
  
  <pre><code>"status": 415,
  "error": "Unsupported Media Type",
  "message": "Content type 'application/json;charset=UTF-8' not supported"
  </code></pre>
  
  <p>I'm using the necessary headers (Accept and Content-Type), so the problem must be the json i'm trying to post</p>
  
  <p>This is the json i get in Postman using GET <a href="http://localhost:8080/state" rel="nofollow noreferrer">http://localhost:8080/state</a>:</p>
  
  <pre><code>{
  "id": 1,
  "name": "California",
  "country_id": {
      "id": 1,
      "name": "US",
   }
  }
  </code></pre>
  
  <p>This is the json i want to post <a href="http://localhost:8080/add_state" rel="nofollow noreferrer">http://localhost:8080/add_state</a></p>
  
  <pre><code>{
  "id": 2,
  "name": "Texas",
  "country_id": {
      "id": 1,
      "name": "US",
   }
  }
  </code></pre>
  
  <p>Also is there a way to use only the id of the country instead of all the object in the Post json?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>This didnt work: </p>
  
  <pre><code>produces = { MediaType.APPLICATION_JSON_UTF8_VALUE, 
  MediaType.APPLICATION_JSON_VALUE }, method=RequestMethod.POST)
  </code></pre>
  
  <p>Here is the code of the repository and controller:</p>
  
  <p>Repository</p>
  
  <pre><code>@Repository
  @Transactional
  public class StateRepository {
  
    @PersistenceContext
    EntityManager entityManager;
  
    public StateDTO insert(StateDTO state) {
        return entityManager.merge(state);
    }
  }
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>@RestController
  public class StateController {
  
      @Autowired
      StateRepository repository;
  
      @RequestMapping(value = "/add_state" , method=RequestMethod.POST)
      public void addState(@RequestBody StateDTO state){
          repository.insert(state);
      }
  
  }
  </code></pre>