Template variables with ControllerLinkBuilder

后端 未结 7 1990
生来不讨喜
生来不讨喜 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<RepositoryLinksResource> {
    
        @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
        }
    }
    
    0 讨论(0)
提交回复
热议问题