Spring HATEOAS Resource Assembler和Resource Links包含许多变量 [英] Spring HATEOAS Resource Assembler and Resource Links with many variables

查看:639
本文介绍了Spring HATEOAS Resource Assembler和Resource Links包含许多变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring HATEOAS和Spring堆栈处理REST API,我在链接到资源方面遇到了一些问题。

I'm working on REST API with Spring HATEOAS and the Spring stack, and i have some problems with links into resources.

这是我的代码:

控制器:

@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。

As you can see, my controller have some parameters in the URI : idInt and idRib.

因此,为了制作一个SelfLink,我必须知道这个参数是否像/ apporteurs / 1234 / ribs / 1234,但我认为汇编程序只需要一个参数和一个简单URI。

So for make a SelfLink, i have to know this parameters for make something like "/apporteurs/1234/ribs/1234", but i think the Assembler want only one parameter, and a "simple" 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'

所以我正在寻找一个优雅的解决方案,因为我找不到什么^^

So i'm looking for an elegant solution to do this, because i didn't find anything ^^

我看到了ResourceProcessor,但我没有使用Spring Data Rest。

I saw something with ResourceProcessor, but i'm not using Spring Data Rest.

可以你帮我 ?提前谢谢;)

Can you help me ? Thank you in advance ;)

编辑:

结果应为:

_links": {
        "editiqueMandat": {
            "href": "http://localhost:9080/apporteurs/6797/mandats/5822"
        },
        "self": {
                "href": "http://localhost:9080/apporteurs/6797/ribs/1234"
            }
    }


推荐答案

@Override
public RibResource toResource(Rib rib) {
    return createResourceWithId(rib.getId(), rib);
}

createResourceWithId()在内部根据控制器的URL创建自我链接在你的情况下,包含占位符 {idInt} 所以你必须提供一个参数:

createResourceWithId() internally creates a self link based on the controller's URL. In your case that contains the placeholder {idInt} so you would have to provide a parameter for that:

CustomUserDetails user = CurrentUserUtils.getCurrentUser();
return createResourceWithId(rib.getId(), rib, user.getIdInt());

更好的选择是不要调用 createResourceWithId( )。只需将现有的所有内容( instantiateResource()移至 toResource()

The better choice would be not to call createResourceWithId() at all. Just move everything you now have in instantiateResource() to toResource().

这篇关于Spring HATEOAS Resource Assembler和Resource Links包含许多变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆