问题
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