java-具有许多变量的Spring HATEOAS资源汇编器和资源链接
作者:互联网
我正在使用Spring HATEOAS和Spring堆栈来开发REST API,并且在链接到资源时遇到一些问题.
这是我的代码:
控制器:
@RestController
@RequestMapping("/apporteurs/{idInt}/ribs")
public class RibController {
@Autowired
private RibResourceAssembler ribResourceAssembler;
@Autowired
private RibRepository ribRepository;
/**
* Methode GET permettant d'obtenir un Rib par son ID
*
* @param idRib ID du Rib
* @return RibResource
*/
@RequestMapping(value = "/{idRib}", method = RequestMethod.GET)
@ResponseBody
public RibResource getRibById(@PathVariable Long idInt, @PathVariable Long idRib) {
CurrentUserUtils.checkAuthorizationByApporteur(idInt);
return ribResourceAssembler.toResource(ribRepository.getRibById(idRib));
}
}
汇编器:
@Component
public class RibResourceAssembler extends ResourceAssemblerSupport<Rib, RibResource> {
public static final long TMP_IDS = 1234L;
@Autowired
private RibResourceMapper ribResourceMapper;
public RibResourceAssembler() {
super(RibController.class, RibResource.class);
}
@Override
public RibResource toResource(Rib rib) {
return createResourceWithId(rib.getId(), rib);
}
/**
* TODO : mettre le lien vers l'editique Mandat
*
* @param rib Rib à instancier en Resource.
* @return RibResource
*/
@Override
protected RibResource instantiateResource(Rib rib) {
RibResource ribResource = ribResourceMapper.fromRib(rib, rib.getLastMandat());
ribResource.removeLinks();
CustomUserDetails user = CurrentUserUtils.getCurrentUser();
UriComponentsBuilder uriBuilderMandat = linkTo(RibController.class).toUriComponentsBuilder();
String uri = uriBuilderMandat.path("/{idRib}/mandats/{idMandat}").buildAndExpand(user.getIdInt(), rib.getId(), TMP_IDS).toUriString();
Link linkEditiqueMandat = new Link(uri).withRel("editiqueMandat");
UriComponentsBuilder uriBuilderRib = linkTo(RibController.class).toUriComponentsBuilder();
String uriSelf = uriBuilderRib.path("/{idRib}").buildAndExpand(user.getIdInt(), rib.getId()).toUriString();
Link linkUriSelf = new Link(uriSelf).withSelfRel();
ribResource.add(linkEditiqueMandat);
ribResource.add(linkUriSelf);
return ribResource;
}
}
资源:
public class RibResource extends ResourceSupport {
private Long idRib;
private String rum;
private String iban;
private String libelle;
private String dateFin;
private String dateCreation;
private String dateModification;
private String codeOperateurCreation;
private String dateRegulationMandat;
private boolean actif;
private boolean reactivable;
private CodeValueResource modeReglement;
/*Gzetter & setters, etc*/
}
如您所见,我的控制器在URI中有一些参数:idInt和idRib.
因此,对于制作SelfLink,我必须知道用于制作“ / apporteurs / 1234 / ribs / 1234”之类的参数,但是我认为汇编器只需要一个参数和一个“简单” URI.
我有这个堆栈跟踪:
2014-11-25 12:02:09.365 ERROR 20860 --- [nio-9080-exec-1] w.s.m.m.a.ResponseEntityExceptionHandler : Not enough variable values available to expand 'idInt'
所以我正在寻找一种优雅的解决方案,因为我什么也没找到^^
我在ResourceProcessor上看到了一些东西,但是我没有使用Spring Data Rest.
你能帮助我吗 ?先感谢您
标签:rest,spring-hateoas,spring,java,hateoas 来源: https://codeday.me/bug/20191121/2048490.html