13

私はこれを含めて私の応答をしたい:

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

それを達成するのは簡単です:

add(new Link("http://localhost/api/keyMaps{/keyMapId}", "keyMaps"));

しかし、もちろん、次のように ControllerLinkBuilder を使用したいと思います。

add(linkTo(methodOn(KeyMapController.class).getKeyMap("{keyMapId}")).withRel("keyMaps"));

問題は、変数 "{keyMapId}" が UriTemplate コンストラクターに到達するまでに、エンコードされた URL に含まれていることです。

http://localhost/api/keyMaps/%7BkeyMapId%7D

したがって、UriTemplate のコンストラクターは、変数が含まれていると認識しません。

テンプレート変数を使用したいことを ControllerLinkBuilder に納得させるにはどうすればよいですか?

4

7 に答える 7

6

の最新バージョンでspring-hateoasは、次のことができます。

UriComponents uriComponents = UriComponentsBuilder.fromUri(linkBuilder.toUri()).build();
UriTemplate template = new UriTemplate(uriComponents.toUriString())
   .with("keyMapId", TemplateVariable.SEGMENT);

あなたに与える:http://localhost:8080/bla{/keyMapId}",

于 2016-07-04T11:54:23.287 に答える
5

同じ問題に遭遇しました。一般的な回避策は、一連の静的ヘルパーを備えた独自の LinkBuilder クラスを用意することです。テンプレート化されたものは次のようになります。

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)
    );
}

これは、PagedResource のコントローラー応答のパラメーターを公開するためのものです。

次に、コントローラーでこれを呼び出し、必要に応じて withRel を追加します。

于 2014-08-04T20:47:00.050 に答える
0

以前のコメントに基づいて、「一時的な」回避策として、一般的なヘルパー メソッド (spring-hateoas-0.20.0 に対して) を実装しました。実装は RequestParameters のみを考慮しており、最適化または十分にテストされているとは言えません。ただし、同じうさぎの穴を旅している他の貧しい魂にとっては便利かもしれません。

public static Link getTemplatedLink(final Method m, final String rel) {
    DefaultParameterNameDiscoverer disco = new DefaultParameterNameDiscoverer();

    ControllerLinkBuilder builder = ControllerLinkBuilder.linkTo(m.getDeclaringClass(), m);
    UriTemplate uriTemplate = new UriTemplate(UriComponentsBuilder.fromUri(builder.toUri()).build().toUriString());
    Annotation[][] parameterAnnotations = m.getParameterAnnotations();

    int param = 0;
    for (Annotation[] parameterAnnotation : parameterAnnotations) {
        for (Annotation annotation : parameterAnnotation) {
            if (annotation.annotationType().equals(RequestParam.class)) {
                RequestParam rpa = (RequestParam) annotation;
                String parameterName = rpa.name();
                if (StringUtils.isEmpty(parameterName)) parameterName = disco.getParameterNames(m)[param];
                uriTemplate = uriTemplate.with(parameterName, TemplateVariable.VariableType.REQUEST_PARAM);
            }
        }
        param++;
    }
    return new Link(uriTemplate, rel);
}
于 2016-10-11T16:32:16.170 に答える