I have a Country and State table for which I have integrated with Spring Data JPA. I have created a function public Page<CountryDetails> getAllCountryDetails
in my CountryServiceImpl for getting all the Country and the corresponding State details. The service is working fine and is giving me the below output:
{
"content": [
{
"id": 123,
"countryName": "USA",
"countryCode": "USA",
"countryDetails": "XXXXXXXX",
"countryZone": "XXXXXXX",
"states": [
{
"id": 23,
"stateName": "Washington DC",
"countryCode": "USA",
"stateCode": "WAS",
"stateDetails": "XXXXX",
"stateZone": "YYYYYY"
},
{
"id": 24,
"stateName": "Some Other States",
"countryCode": "USA",
"stateCode": "SOS",
"stateDetails": "XXXXX",
"stateZone": "YYYYYY"
}
]
}
],
"last": false,
"totalPages": 28,
"totalElements": 326,
"size": 12,
"number": 0,
"sort": null,
"numberOfElements": 12,
"first": true
}
My Complete code is as given below:
CountryRepository.java
@Repository
public interface CountryRepository extends JpaRepository<CountryDetails, Integer> {
@Query(value = "SELECT country FROM Country country GROUP BY country.countryId ORDER BY ?#{#pageable}",
countQuery = "SELECT COUNT(*) FROM Country country GROUP BY country.countryId ORDER BY ?#{#pageable}")
public Page<CountryDetails> findAll(Pageable pageRequest);
}
CountryServiceImpl.java
@Service
public class CountryServiceImpl implements CountryService {
@Autowired
private CountryRepository countryRepository;
@Override
public Page<CountryDetails> getAllCountryDetails(final int page, final int size) {
return countryRepository.findAll(new PageRequest(page, size));
}
}
CountryDetails.java
@Entity
@Table(name = "country", uniqueConstraints = @UniqueConstraint(columnNames = "id"))
public class CountryDetails {
@Id
@GeneratedValue
@Column(name = "id", unique = true, nullable = false)
private Integer id;
private String countryName;
private String countryCode;
private String countryDetails;
private String countryZone;
@JsonManagedReference
@OneToMany(fetch = FetchType.LAZY, mappedBy = "countryDetails")
private List<State> states;
// getters / setters omitted
}
State.java
@Entity
@Table(name = "state", uniqueConstraints = @UniqueConstraint(columnNames = "id"))
public class State {
@Id
@GeneratedValue
@Column(name = "id", unique = true, nullable = false)
private Integer id;
private String stateName;
private String countryCode;
private String stateCode;
private String stateDetails;
private String stateZone;
@JsonBackReference
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "countryCode", nullable = false, insertable = false, updatable = false, foreignKey = @javax.persistence.ForeignKey(name="none",value = ConstraintMode.NO_CONSTRAINT))
private CountryDetails countryDetails;
// getters / setters omitted
}
Actually what I want the country service to return with minimal information like as shown below
{
"content": [
{
"countryName": "USA",
"countryCode": "USA",
"states": [
{
"stateCode": "WAS"
},
{
"stateCode": "SOS"
}
]
}
],
"last": false,
"totalPages": 28,
"totalElements": 326,
"size": 12,
"number": 0,
"sort": null,
"numberOfElements": 12,
"first": true
}
So for achieving that I have used Projections like as shown below
CountryProjection .java
public interface CountryProjection {
public String getCountryName();
public String getCountryCode();
public List<StateProjection> getStates();
}
StateProjection .java
public interface StateProjection {
public String getStateCode();
}
CountryServiceImpl.java
@Repository
public interface CountryRepository extends JpaRepository<CountryDetails, Integer> {
@Query(value = "SELECT country.countryName AS countryName, country.countryCode AS countryCode FROM Country country GROUP BY country.countryId ORDER BY ?#{#pageable}",
countQuery = "SELECT COUNT(*) FROM Country country GROUP BY country.countryId ORDER BY ?#{#pageable}")
public Page<CountryProjection> findAll(Pageable pageRequest);
}
But now the service is returning any of the state details like as shown below
{
"content": [
{
"countryName": "USA",
"countryCode": "USA"
}
],
"last": false,
"totalPages": 28,
"totalElements": 326,
"size": 12,
"number": 0,
"sort": null,
"numberOfElements": 12,
"first": true
}
How can we get the minimal state details also like as shown below
{
"content": [
{
"countryName": "USA",
"countryCode": "USA",
"states": [
{
"stateCode": "WAS"
},
{
"stateCode": "SOS"
}
]
}
],
"last": false,
"totalPages": 28,
"totalElements": 326,
"size": 12,
"number": 0,
"sort": null,
"numberOfElements": 12,
"first": true
}
Can anyone please help me on this
Try using JsonIgnore with the fields which you don't want in the return JSON
@JsonIgnore
private String stateDetails;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With