我正在使用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”那样,但我认为Assembler只需要一个参数和一个“简单”的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。
你能帮帮我吗?提前谢谢你;)编辑:
结果应为:
_links": {
"editiqueMandat": {
"href": "http://localhost:9080/apporteurs/6797/mandats/5822"
},
"self": {
"href": "http://localhost:9080/apporteurs/6797/ribs/1234"
}
}
答案 0 :(得分:3)
@Override
public RibResource toResource(Rib rib) {
return createResourceWithId(rib.getId(), rib);
}
createResourceWithId()
在内部根据控制器的URL创建自我链接。在包含占位符{idInt}
的情况下,您必须为其提供参数:
CustomUserDetails user = CurrentUserUtils.getCurrentUser();
return createResourceWithId(rib.getId(), rib, user.getIdInt());
更好的选择是根本不打电话给createResourceWithId()
。只需将您现在拥有的所有内容instantiateResource()
移至toResource()
。