Reputation: 12417
I have the following entities provided below,
@Entity(name = "Employee")
@Table(name = "employee")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "creation_on" ,updatable = false)
private Date creationOn;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
@Column(name = "phoneNumber")
private String phoneNumber;
@Column(name = "age")
private Integer age;
@Column(name = "state")
@Enumerated(EnumType.STRING)
private EmployeeStates employeeState;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "address_id", referencedColumnName = "id")
private Address address;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity(name = "address")
@Table(name = "address")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "street")
private String street;
@Column(name = "state")
private String state;
@Column(name = "country")
private String country;
@Column(name = "zip")
private int zip;
@OneToOne(mappedBy = "address")
private Employee employee;
}
The POST call is provided below,
@PostMapping(value = "/create")
public ResponseEntity<Object> createEmployee(@Valid @RequestBody EmployeeDto employeeDto) {
try{
Employee employee = employeeService.createEmployee(employeeDto);
if(employee != null){
return new ResponseEntity<>(employee, new HttpHeaders(), HttpStatus.CREATED);
}
return new ResponseEntity<>(ApiResponseMessage.getGenericApiResponse(Boolean.FALSE, HttpStatus.UNPROCESSABLE_ENTITY,
MessageConstant.EMPLOYEE_NOT_CREATE_MSG), new HttpHeaders(), HttpStatus.UNPROCESSABLE_ENTITY);
}
catch (Exception ex) {
log.error(MessageConstant.INTERNAL_SERVER_ERROR_MSG + ex.getMessage());
return new ResponseEntity<>(ApiResponseMessage.getInternalServerError(), new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
When the response is coming, its below:
{
"id": 2,
"creationOn": "2021-02-27T15:05:23.585+00:00",
"firstName": "string",
"lastName": "string",
"email": "string",
"phoneNumber": "string",
"age": 0,
"employeeState": "ADDED",
"address": {
"id": 3,
"street": "string",
"state": "string",
"country": "string",
"zip": 0,
"employee": null
}
}
How do I not see the "employee": null in the response?
Upvotes: 1
Views: 59
Reputation: 12417
I need to ignore JSON value from the child entity as below:
@OneToOne(mappedBy = "address")
@JsonIgnore
private Employee employee;
This makes all fine.
Upvotes: 0
Reputation: 3766
You need to use @JsonInclude(Include.NON_EMPTY)
or @JsonInclude(Include.NON_NULL)
on your Entity
classes.
Like
@Entity(name = "Employee")
@Table(name = "employee")
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(Include.NON_EMPTY)
public class Employee {
}
@JsonInclude(Include.NON_EMPTY)
-
Value that indicates that only properties with null value,or what is considered empty, are not to be included.Definition of emptiness is data type specific; see belowfor details on actual handling.
@JsonInclude(Include.NON_NULL)
- Value that indicates that only properties with non-nullvalues are to be included.
Upvotes: 1