As mentioned, I would like to get help understanding this error which is quite cryptic to me:
WARN 6436 --- [nio-8080-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Embedded wrapper org.springframework.hateoas.server.core.EmbeddedWrappers$EmbeddedCollection@7538c537 returned null for both the static rel and the rel target type! Make sure one of the two returns a non-null value!; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Embedded wrapper org.springframework.hateoas.server.core.EmbeddedWrappers$EmbeddedCollection@7538c537 returned null for both the static rel and the rel target type! Make sure one of the two returns a non-null value! (through reference chain: org.springframework.hateoas.CollectionModel["_embedded"])]
This error came about after I tried accessing a collection model through the following method within my Spring Controller. What I'm trying to do is to return all the share transactions associated with a particular stock code.
@Autowired
private ShareTransactionAssembler shareTransactionAssembler;
@GetMapping("/{stockCode}/transactions")
public CollectionModel<ShareTransactionModel> getShareTransactions (@PathVariable("stockCode") String stockCodeString) {
StockCode stockCode = new StockCode(stockCodeString);
List<ShareTransaction> shareTransactionList = stockQueryService.getShareTransactions(stockCode);
return shareTransactionAssembler.toCollectionModel(shareTransactionList, stockCode);
}
The following is my shareTransactionAssembler code:
@Component
public class ShareTransactionAssembler extends RepresentationModelAssemblerSupport<ShareTransaction, ShareTransactionModel> {
public ShareTransactionAssembler() {
super(StockController.class, ShareTransactionModel.class);
}
@Override
public ShareTransactionModel toModel(ShareTransaction shareTransaction) {
String stockCodeString = shareTransaction.getStock().getStockCode().getStockCodeAsString();
Link selfLink = linkTo(
methodOn(StockController.class)
.findShareTransaction(stockCodeString, shareTransactionModel.getId())
)
.withSelfRel();
shareTransactionModel.add(selfLink);
Link stockLink = linkTo(
methodOn(StockController.class)
.findStock(stockCodeString)
)
.withRel("Stock");
shareTransactionModel.add(stockLink);
return shareTransactionModel;
}
public CollectionModel<ShareTransactionModel> toCollectionModel (List<ShareTransaction> shareTransactionList, StockCode stockCode) {
CollectionModel<ShareTransactionModel> shareTransactionModels = super.toCollectionModel(shareTransactionList);
Link selfLink = linkTo(
methodOn(StockController.class)
.getShareTransactions(stockCode.getStockCodeAsString())
)
.withSelfRel();
shareTransactionModels.add(selfLink);
return shareTransactionModels;
}
}
In case you need to know, accessing the individual share transactions seem to work fine. An example JSON response is as follows:
{
"id": 1,
"transactPrice": "13.00",
"commissionPaid": "1.05",
"transactTimeDate": "2020-03-07T13:05",
"transactUnits": 30,
"_links": {
"self": {
"href": "http://localhost:8080/stock/NVDA/transactions/1"
},
"Stock": {
"href": "http://localhost:8080/stock/NVDA"
}
}
}
I would really appreciate if you can shed some light on this! Thanks!