Spring-Hateoas: exception in creating a new link

二次信任 提交于 2020-01-04 05:35:13

问题


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.


回答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


来源:https://stackoverflow.com/questions/38548834/spring-hateoas-exception-in-creating-a-new-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!