1

Usually CollectionModel will return an _embedded array, but in this example:

@GetMapping("/{id}/productMaterials")
    public ResponseEntity<?> getProductMaterials(@PathVariable Integer id) {
        Optional<Material> optionalMaterial = materialRepository.findById(id);
        if (optionalMaterial.isPresent()) {
            List<ProductMaterial> productMaterials = optionalMaterial.get().getProductMaterials();
            CollectionModel<ProductMaterialModel> productMaterialModels =
                    new ProductMaterialModelAssembler(ProductMaterialController.class, ProductMaterialModel.class).
                            toCollectionModel(productMaterials);
            return ResponseEntity.ok().body(productMaterialModels);
        }
        return ResponseEntity.badRequest().body("no such material");
    }

if the productMaterials is empty CollectionModel will not render the _embedded array which will break the client. Is there any ways to fix this?

2
  • you will have to use an EmbeddedWrapper stackoverflow.com/questions/30286795/… you should mark you resource explicitly to render an empty _embedded array Commented Nov 1, 2020 at 8:44
  • Thank you for your answer. Could you show me how to write it? I have no idea what the author was talking about :(( Commented Nov 1, 2020 at 9:03

2 Answers 2

3
if (optionalMaterial.isPresent()) {
        List<ProductMaterial> productMaterials = optionalMaterial.get().getProductMaterials();
        CollectionModel<ProductMaterialModel> productMaterialModels =
                new ProductMaterialModelAssembler(ProductMaterialController.class, ProductMaterialModel.class).
                        toCollectionModel(productMaterials);
        if(productMaterialModels.isEmpty()) {
            EmbeddedWrappers wrappers = new EmbeddedWrappers(false);
            EmbeddedWrapper wrapper = wrappers.emptyCollectionOf(ProductMaterialModel.class);
            Resources<Object> resources = new Resources<>(Arrays.asList(wrapper));
            return ResponseEntity.ok(new Resources<>(resources));
        } else {
            return ResponseEntity.ok().body(productMaterialModels);
        }
    }    
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. it worked :)) since the Resources does not exist anymore, i change the code a little bit
Is spring HATEOAS popular right now? All the solutions seem to be deprecated and not elegant at all.
@LaHai how did you change the code? I can't find the Resources class.
@Raman Happy new year. I believe it's CollectionModel now. You can find the changes in here
0

It could be easily done now using this code:

    List<ProductMaterial> productMaterials = optionalMaterial.get().getProductMaterials();
    return HalModelBuilder.emptyHalModel()
            .embed(productMaterials, ProductMaterial.class)
            .build();

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.