Template variables with ControllerLinkBuilder

后端 未结 7 2009
生来不讨喜
生来不讨喜 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:20

    I needed to include a link with template variables in the root of a spring data rest application, to get access via traverson to an oauth2 token. This is working fine, maybe useful:

    @Component
    class RepositoryLinksResourceProcessor implements ResourceProcessor {
    
        @Override
        RepositoryLinksResource process(RepositoryLinksResource resource) {
    
            UriTemplate uriTemplate =  new UriTemplate(
                    ControllerLinkBuilder.
                            linkTo(
                                    TokenEndpoint,
                                    TokenEndpoint.getDeclaredMethod("postAccessToken", java.security.Principal, Map )).
                            toUriComponentsBuilder().
                            build().
                            toString(),
                    new TemplateVariables([
                            new TemplateVariable("username", TemplateVariable.VariableType.REQUEST_PARAM),
                            new TemplateVariable("password", TemplateVariable.VariableType.REQUEST_PARAM),
                            new TemplateVariable("clientId", TemplateVariable.VariableType.REQUEST_PARAM),
                            new TemplateVariable("clientSecret", TemplateVariable.VariableType.REQUEST_PARAM)
                    ])
            )
    
            resource.add(
                    new Link( uriTemplate,
                            "token"
                    )
            )
    
            return resource
        }
    }
    

提交回复
热议问题