Spring-Hateoas: exception in creating a new link
Asked Answered
C

2

2

Overview:

I am going to add a new link based on Spring-Hateoas-Doc to the JSON response by using the following command:

linkTo(methodOn(ProductRepository.class).findOne(10L)).withRel("product");

Problem:

However I got the following exception:

java.lang.IllegalArgumentException: 'uriTemplate' must not be null

So I would be grateful if anyone could suggest me a genuine solution.

Cholecystotomy answered 24/7, 2016 at 4:44 Comment(0)
C
1

I found the issue. As I my processor class is not a rest controller, this issue has been raised. To solve it , I used the entityLinks instead, as follows:

@Controller
public class StockMovementsProcessor implements ResourceProcessor<Resource<StockMovementsProjection>> {
    @Autowired
    private EntityLinks entityLinks;

    @Override
    public Resource<StockMovementsProjection> process(Resource<StockMovementsProjection> stockMovementsProjectionResource) {
        StockMovementsProjection stockMovementsProjection = stockMovementsProjectionResource.getContent();

        stockMovementsProjectionResource.add(entityLinks.linkFor(Product.class).slash(10L).withRel("product"));

        return stockMovementsProjectionResource;
    }
}

And it created the following link for me:

http://localhost/products/10
Cholecystotomy answered 25/7, 2016 at 0:45 Comment(1)
this might be because of hateos version issue.Feverous
F
0

if you are using hateos version 0.20.0 then try upgrading it to 23 using below maven dependency

<dependency>
    <groupId>org.springframework.hateoas</groupId>
    <artifactId>spring-hateoas</artifactId>
    <version>0.23.0.RELEASE</version>
</dependency>
Feverous answered 11/4, 2020 at 20:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.