Template variables with ControllerLinkBuilder

后端 未结 7 2024
生来不讨喜
生来不讨喜 2021-02-13 17:54

I want my response to include this:

\"keyMaps\":{
  \"href\":\"http://localhost/api/keyMaps{/keyMapId}\",
  \"templated\":true
 }

That\'s easy

7条回答
  •  忘掉有多难
    2021-02-13 18:09

    We've run into the same problem. General workaround is we have our own LinkBuilder class with a bunch of static helpers. Templated ones look like this:

    public static Link linkToSubcategoriesTemplated(String categoryId){
    
        return new Link(
            new UriTemplate(
                linkTo(methodOn(CategoryController.class).subcategories(null, null, categoryId))
                    .toUriComponentsBuilder().build().toUriString(),
                // register it as variable
                getBaseTemplateVariables()
            ),
            REL_SUBCATEGORIES
        );
    }
    
    private static TemplateVariables getBaseTemplateVariables() {
        return new TemplateVariables(
            new TemplateVariable("page", TemplateVariable.VariableType.REQUEST_PARAM),
            new TemplateVariable("sort", TemplateVariable.VariableType.REQUEST_PARAM),
            new TemplateVariable("size", TemplateVariable.VariableType.REQUEST_PARAM)
        );
    }
    

    This is for exposing the parameters of a controller response of a PagedResource.

    then in the controllers we call this an append a withRel as needed.

提交回复
热议问题