gpt4 book ai didi

java - Spring Projections 不返回状态详细信息

转载 作者:IT老高 更新时间:2023-10-28 23:58:24 25 4
gpt4 key购买 nike

我有一个国家和州表,我已将其与 Spring Data JPA 集成。我创建了一个函数 public Page<CountryDetails> getAllCountryDetails在我的 CountryServiceImpl 中获取所有国家和相应的州详细信息。该服务运行良好,并为我提供以下输出:

{
"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
}

我的完整代码如下:

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
}

现在是问题

实际上我希望国家/地区服务返回的信息最少,如下所示

{
"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
}

因此,为了实现这一点,我使用了如下所示的投影

国家投影.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);
}

但现在服务正在返回任何状态详细信息,如下所示

{
"content": [
{
"countryName": "USA",
"countryCode": "USA"
}
],
"last": false,
"totalPages": 28,
"totalElements": 326,
"size": 12,
"number": 0,
"sort": null,
"numberOfElements": 12,
"first": true
}

我们怎样才能得到最小的状态细节,如下所示

{
"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
}

谁能帮我解决这个问题

最佳答案

尝试将 JsonIgnore 与您在返回 JSON 中不需要的字段一起使用

@JsonIgnore
private String stateDetails;

关于java - Spring Projections 不返回状态详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50630056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com