0

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!

2 Answers 2

3

I stumbled onto this error as well. I got it fixed by extending my model class with RepresentationModel instead of EntityModel.

Still searching why this makes a difference..

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, that did the trick! Just like you, I also don't know what the difference is, but you've really helped me resolve a huge thorn in my side. Hopefully someone else more experienced can shed light on this issue.
1

I had this problem too and this is my understanding based on reading the documentation here (see section 2.4): https://docs.spring.io/spring-hateoas/docs/current/reference/html/#reference

You are not supposed to directly inherit from EntityModel. EntityModel is a generic RepresentationModel implementation that can be used for representing singular objects or concepts. You can use it when you don't want to write your own RepresentationModel. It should just serialize all fields in the object you pass it.

RepresentationModel is the root class for defining your own representation models.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.