在我目前的实施中使用Spring-Boot,-HATEOAS,-Rest-Data我试图节省一些进一步的休息呼叫并增加我的休息资源以获得信用额度以提供信用关系(见下面的帐户为ManyToOne和creditBookingClassPayments为OneToMany)。 现在的问题是我无法让它运行。通话始终提供空关系。我真的很感激你的帮助。
以下是周围环境:
Credit.java
@Entity
@Getter
@Setter
public class Credit {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Setter(NONE)
@Column(name = "id")
private Long itemId;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="account_id", nullable = false)
private Account account;
@OneToMany(mappedBy = "credit")
private List<CreditBookingClassPayment> creditBookingClassPayments = new ArrayList<>();
@NotNull(message="Please enter a valid short name.")
@Column(length = 10, nullable = false)
private String shortName;
@NotNull(message="Please enter a valid name.")
@Column(nullable = false)
private String name;
...
}
CreditRepositoryCustomImpl.java
使用QueryDsl通过其realation来增强信用资源
...
@Override
public List<Credit> findDistinctByAccountItemIdNew(Long accountId) {
QCredit credit = QCredit.credit;
QAccount account = QAccount.account;
QCreditBookingClassPayment creditBookingClassPayment = QCreditBookingClassPayment.creditBookingClassPayment;
QBookingClass bookingClass = QBookingClass.bookingClass;
BooleanExpression hasAccountItemId = credit.account.itemId.eq(accountId);
List<Credit> credits = from(credit).where(hasAccountItemId)
.innerJoin(credit.account, account)
.leftJoin(credit.creditBookingClassPayments, creditBookingClassPayment)
.leftJoin(creditBookingClassPayment.bookingClass, bookingClass).groupBy(credit.itemId).fetch();
return credits;
}
...
CreditController.java
在此处查看responseBody所有(帐户和信用卡付款)可用于信用
@RepositoryRestController
public class CreditController {
@Autowired
private CreditRepository creditRepository;
@RequestMapping(value = "/credit/search/findAllByAccountItemIdNew", method= RequestMethod.GET, produces = MediaTypes.HAL_JSON_VALUE)
@ResponseBody
public ResponseEntity<Resources<PersistentEntityResource>> findAllByAccountItemIdNew(@RequestParam Long accountId, PersistentEntityResourceAssembler persistentEntityResourceAssembler) {
List<Credit> credits = creditRepository.findDistinctByAccountItemIdNew(accountId);
Resources<PersistentEntityResource> responseBody = new Resources<PersistentEntityResource>(credits.stream()
.map(persistentEntityResourceAssembler::toResource)
.collect(Collectors.toList()));
return ResponseEntity.ok(responseBody);
}
}
CreditResourceIntegrTest.java
这里creditResourcesEntity持有信用但账户为空且creditBookingClassPayment是一个空数组
@Test
public void testFindAllByAccountItemId() throws URISyntaxException {
URIBuilder builder = new URIBuilder(creditFindAllByAccountItemIdRestUrl);
builder.addParameter("accountId", String.valueOf(EXPECTED_ACCOUNT_ID));
builder.addParameter("projection", "base");
RequestEntity<Void> request = RequestEntity.get(builder.build())
.accept(MediaTypes.HAL_JSON).acceptCharset(Charset.forName("UTF-8")).build();
ResponseEntity<Resources<Resource<Credit>>> creditResourcesEntity =
restTemplate.exchange(request, new ParameterizedTypeReference<Resources<Resource<Credit>>>() {});
assertEquals(HttpStatus.OK, creditResourcesEntity.getStatusCode());
//assertEquals(EXPECTED_CREDIT_COUNT, creditResourcesEntity.getBody().getContent().size());
}
我错过了什么吗?
感谢您的帮助! 卡斯滕
答案 0 :(得分:0)
好的,PersistentEntityResourceAssembler不支持关系。但这可以通过使用预测来处理。
CreditProjection.java
@Projection(name = "base" , types = Credit.class)
public interface CreditProjection {
String getShortName();
String getName();
List<CreditBookingClassPaymentProjection> getCreditBookingClassPayments();
BigDecimal getValue();
BigDecimal getInterestRate();
BigDecimal getMonthlyRate();
}
CreditBookingClassPaymentProjection.java
@Projection(name = "base" , types = CreditBookingClassPayment.class)
public interface CreditBookingClassPaymentProjection {
BookingClass getBookingClass();
CreditPaymentType getCreditPaymentType();
}
CreditController.java
@RepositoryRestController
public class CreditController {
@Autowired
private ProjectionFactory projectionFactory;
@Autowired
private CreditRepository creditRepository;
@RequestMapping(value = "/credit/search/findAllByAccountItemIdNew", method = RequestMethod.GET, produces = MediaTypes.HAL_JSON_VALUE)
@ResponseBody
public ResponseEntity<Resources<?>> findAllByAccountItemIdNew(@RequestParam Long accountId,
PersistentEntityResourceAssembler persistentEntityResourceAssembler) {
List<Credit> credits = creditRepository.findDistinctByAccountItemIdNew(accountId);
List<PersistentEntityResource> creditResources = new ArrayList<>();
for (Credit credit : credits) {
// credit.getCreditBookingClassPayments()
PersistentEntityResource creditResource = persistentEntityResourceAssembler.toResource(credit);
creditResources.add(creditResource);
}
Resources<CreditProjection> responseBody = new Resources<CreditProjection>(credits.stream()
.map(credit -> projectionFactory.createProjection(CreditProjection.class, credit))
.collect(Collectors.toList()));
return ResponseEntity.ok(responseBody);
}
}