I'm developing a REST API using java and spring boot.
There are two entities with One-to-One relation:
User:
@Entity
@Table(name = "users")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "first_name", nullable = false)
private String firstName;
@Column(name = "last_name", nullable = false)
private String lastName;
@Column(name = "email", nullable = false, unique = true)
private String email;
@OneToOne(fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
mappedBy = "employee")
@JsonBackReference
private Company company;
// constructors, getters & setters...
And Company:
@Entity
@Table(name = "companies")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "company_name", nullable = false)
private String companyName;
@Column(name = "company_info", nullable = false)
private String companyInfo;
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_at", nullable = false)
private Date createdAt;
@OneToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "employee_id", nullable = false)
@JsonManagedReference
private User employee;
// constructors, getters & setters...
I want to be able to create Companies via POST method so I've made one in CompanyController (I omit GET methods in controller as they work as expected):
@RestController
@RequestMapping(value = "/api/v1")
public class CompanyController {
private static final Logger logger = LoggerFactory.getLogger(CompanyController.class);
@Autowired
private CompanyRepository companyRepository;
@PostMapping(path = "company", produces = "application/JSON")
public ResponseEntity<?> createCompany(@RequestBody Company company){
logger.info("Request to save new Company: {}", company);
Company result = companyRepository.save(company);
return ResponseEntity.ok().body(result);
}
The createCompany
method works fine when I send request with JSON like below:
{
"companyName" : "Big Client Company 2",
"companyInfo" : "unspecified",
"employee" : {
"id": 17,
"firstName": "someName",
"lastName": "someLastName",
"email": "someEmail@a.ru"
}
}
However, I want to be able to send JSONs without whole body of employee field but with just an id:
{
"companyName" : "Big Client Company 2",
"companyInfo" : "unspecified",
"employee" : 17
}
When I do like this I get an error:
JSON parse error: Cannot construct instance of
model.User
(although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (17)
So the question is there any way to do this without changing Company class "employee" to String (and getting rid of One-to-One relation)?
I've tried to find examples of writing custom JSON deserializers but haven't succeeded with any suitable for me.