Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何将特定rel上的链接显示为数组,即使只有一个链接_Spring_Spring Mvc_Spring Hateoas - Fatal编程技术网

Spring 如何将特定rel上的链接显示为数组,即使只有一个链接

Spring 如何将特定rel上的链接显示为数组,即使只有一个链接,spring,spring-mvc,spring-hateoas,Spring,Spring Mvc,Spring Hateoas,我想通过“persons”rel返回一个链接数组。若我有多个人,那个么一切都没问题,但若我只有一个人,那个么它将返回一个元素,而我的客户机代码期望数组失败 在18岁的春季不可能。为此,我们重载了内置序列化程序。这是非常恶劣的 从技术上讲,客户机应该将rel:{}解释为rel:[{}]是HAL兼容的..但是他们很少这样做 您必须删除并覆盖内置的HATEOAS one,我们是这样做的,但这实际上删除了所有其他转换器: for (Person person : company.getPersons()

我想通过“persons”rel返回一个链接数组。若我有多个人,那个么一切都没问题,但若我只有一个人,那个么它将返回一个元素,而我的客户机代码期望数组失败

在18岁的春季不可能。为此,我们重载了内置序列化程序。这是非常恶劣的

从技术上讲,客户机应该将rel:{}解释为rel:[{}]是HAL兼容的..但是他们很少这样做

您必须删除并覆盖内置的HATEOAS one,我们是这样做的,但这实际上删除了所有其他转换器:

for (Person person : company.getPersons()) {
    resource.add(linkTo(methodOn(PersonController.class).view(person.getId()))
      .withRel("persons"));
}

您可以使用它来注释资源类中的getLinks()方法,这在spring hateoas 18中是不可能的。为此,我们重载了内置序列化程序。这是非常恶劣的

从技术上讲,客户机应该将rel:{}解释为rel:[{}]是HAL兼容的..但是他们很少这样做

您必须删除并覆盖内置的HATEOAS one,我们是这样做的,但这实际上删除了所有其他转换器:

for (Person person : company.getPersons()) {
    resource.add(linkTo(methodOn(PersonController.class).view(person.getId()))
      .withRel("persons"));
}

您可以使用它来注释资源类中的getLinks()方法,这在spring hateoas 18中是不可能的。为此,我们重载了内置序列化程序。这是非常恶劣的

从技术上讲,客户机应该将rel:{}解释为rel:[{}]是HAL兼容的..但是他们很少这样做

您必须删除并覆盖内置的HATEOAS one,我们是这样做的,但这实际上删除了所有其他转换器:

for (Person person : company.getPersons()) {
    resource.add(linkTo(methodOn(PersonController.class).view(person.getId()))
      .withRel("persons"));
}

您可以使用它来注释资源类中的getLinks()方法,这在spring hateoas 18中是不可能的。为此,我们重载了内置序列化程序。这是非常恶劣的

从技术上讲,客户机应该将rel:{}解释为rel:[{}]是HAL兼容的..但是他们很少这样做

您必须删除并覆盖内置的HATEOAS one,我们是这样做的,但这实际上删除了所有其他转换器:

for (Person person : company.getPersons()) {
    resource.add(linkTo(methodOn(PersonController.class).view(person.getId()))
      .withRel("persons"));
}

您可以使用它来注释资源类中的getLinks()方法

对于这个问题,我有一个与Chris的答案类似的解决方法。主要区别在于我没有扩展
Jackson2HalModule
,而是创建了一个新的处理程序实例化器,并将其设置为我自己创建的
Jackson2HalModule
的新实例的处理程序实例化器。我希望SpringHateoas最终能够在本地支持这一功能;我有一个尝试这样做的方法。以下是我如何实施我的变通方法:

步骤1:创建mixin类:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ForceMultiLink {

    String[] value();

}
步骤3:创建新的序列化程序,该序列化程序将覆盖Spring HATEOAS的链接列表序列化程序:

public class HalMultipleLinkRels {
    private final Set<String> rels;

    public HalMultipleLinkRels(String... rels) {
        this.rels = new HashSet<String>(Arrays.asList(rels));
    }

    public Set<String> getRels() {
        return Collections.unmodifiableSet(rels);
    }
}
此实例化器将控制自定义序列化程序的生命周期。它维护一个内部实例
Jackson2HalModule.halhandler实例化器
,并将所有其他序列化程序委托给该实例

第5步:将所有内容放在一起:

public class HalHandlerInstantiator extends HandlerInstantiator {

    private final Jackson2HalModule.HalHandlerInstantiator halHandlerInstantiator;
    private final Map<Class<?>, JsonSerializer<?>> serializerMap = new HashMap<>();

    public HalHandlerInstantiator(RelProvider relProvider, CurieProvider curieProvider, HalMultipleLinkRels halMultipleLinkRels) {
        this(relProvider, curieProvider, halMultipleLinkRels, true);
    }

    public HalHandlerInstantiator(RelProvider relProvider, CurieProvider curieProvider, HalMultipleLinkRels halMultipleLinkRels, boolean enforceEmbeddedCollections) {
        halHandlerInstantiator = new Jackson2HalModule.HalHandlerInstantiator(relProvider, curieProvider, enforceEmbeddedCollections);

        serializerMap.put(HalLinkListSerializer.class, new HalLinkListSerializer(curieProvider, halMultipleLinkRels));
    }

    @Override
    public JsonDeserializer<?> deserializerInstance(DeserializationConfig config, Annotated annotated, Class<?> deserClass) {
        return halHandlerInstantiator.deserializerInstance(config, annotated, deserClass);
    }

    @Override
    public KeyDeserializer keyDeserializerInstance(DeserializationConfig config, Annotated annotated, Class<?> keyDeserClass) {
        return halHandlerInstantiator.keyDeserializerInstance(config, annotated, keyDeserClass);
    }

    @Override
    public JsonSerializer<?> serializerInstance(SerializationConfig config, Annotated annotated, Class<?> serClass) {
        if(serializerMap.containsKey(serClass)) {
            return serializerMap.get(serClass);
        } else {
            return halHandlerInstantiator.serializerInstance(config, annotated, serClass);
        }
    }

    @Override
    public TypeResolverBuilder<?> typeResolverBuilderInstance(MapperConfig<?> config, Annotated annotated, Class<?> builderClass) {
        return halHandlerInstantiator.typeResolverBuilderInstance(config, annotated, builderClass);
    }

    @Override
    public TypeIdResolver typeIdResolverInstance(MapperConfig<?> config, Annotated annotated, Class<?> resolverClass) {
        return halHandlerInstantiator.typeIdResolverInstance(config, annotated, resolverClass);
    }
}

我有一个解决这个问题的方法,与Chris的答案类似。主要区别在于我没有扩展
Jackson2HalModule
,而是创建了一个新的处理程序实例化器,并将其设置为我自己创建的
Jackson2HalModule
的新实例的处理程序实例化器。我希望SpringHateoas最终能够在本地支持这一功能;我有一个尝试这样做的方法。以下是我如何实施我的变通方法:

步骤1:创建mixin类:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ForceMultiLink {

    String[] value();

}
步骤3:创建新的序列化程序,该序列化程序将覆盖Spring HATEOAS的链接列表序列化程序:

public class HalMultipleLinkRels {
    private final Set<String> rels;

    public HalMultipleLinkRels(String... rels) {
        this.rels = new HashSet<String>(Arrays.asList(rels));
    }

    public Set<String> getRels() {
        return Collections.unmodifiableSet(rels);
    }
}
此实例化器将控制自定义序列化程序的生命周期。它维护一个内部实例
Jackson2HalModule.halhandler实例化器
,并将所有其他序列化程序委托给该实例

第5步:将所有内容放在一起:

public class HalHandlerInstantiator extends HandlerInstantiator {

    private final Jackson2HalModule.HalHandlerInstantiator halHandlerInstantiator;
    private final Map<Class<?>, JsonSerializer<?>> serializerMap = new HashMap<>();

    public HalHandlerInstantiator(RelProvider relProvider, CurieProvider curieProvider, HalMultipleLinkRels halMultipleLinkRels) {
        this(relProvider, curieProvider, halMultipleLinkRels, true);
    }

    public HalHandlerInstantiator(RelProvider relProvider, CurieProvider curieProvider, HalMultipleLinkRels halMultipleLinkRels, boolean enforceEmbeddedCollections) {
        halHandlerInstantiator = new Jackson2HalModule.HalHandlerInstantiator(relProvider, curieProvider, enforceEmbeddedCollections);

        serializerMap.put(HalLinkListSerializer.class, new HalLinkListSerializer(curieProvider, halMultipleLinkRels));
    }

    @Override
    public JsonDeserializer<?> deserializerInstance(DeserializationConfig config, Annotated annotated, Class<?> deserClass) {
        return halHandlerInstantiator.deserializerInstance(config, annotated, deserClass);
    }

    @Override
    public KeyDeserializer keyDeserializerInstance(DeserializationConfig config, Annotated annotated, Class<?> keyDeserClass) {
        return halHandlerInstantiator.keyDeserializerInstance(config, annotated, keyDeserClass);
    }

    @Override
    public JsonSerializer<?> serializerInstance(SerializationConfig config, Annotated annotated, Class<?> serClass) {
        if(serializerMap.containsKey(serClass)) {
            return serializerMap.get(serClass);
        } else {
            return halHandlerInstantiator.serializerInstance(config, annotated, serClass);
        }
    }

    @Override
    public TypeResolverBuilder<?> typeResolverBuilderInstance(MapperConfig<?> config, Annotated annotated, Class<?> builderClass) {
        return halHandlerInstantiator.typeResolverBuilderInstance(config, annotated, builderClass);
    }

    @Override
    public TypeIdResolver typeIdResolverInstance(MapperConfig<?> config, Annotated annotated, Class<?> resolverClass) {
        return halHandlerInstantiator.typeIdResolverInstance(config, annotated, resolverClass);
    }
}

我有一个解决这个问题的方法,与Chris的答案类似。主要区别在于我没有扩展
Jackson2HalModule
,而是创建了一个新的处理程序实例化器,并将其设置为我自己创建的
Jackson2HalModule
的新实例的处理程序实例化器。我希望SpringHateoas最终能够在本地支持这一功能;我有一个尝试这样做的方法。以下是我如何实施我的变通方法:

步骤1:创建mixin类:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ForceMultiLink {

    String[] value();

}
步骤3:创建新的序列化程序,该序列化程序将覆盖Spring HATEOAS的链接列表序列化程序:

public class HalMultipleLinkRels {
    private final Set<String> rels;

    public HalMultipleLinkRels(String... rels) {
        this.rels = new HashSet<String>(Arrays.asList(rels));
    }

    public Set<String> getRels() {
        return Collections.unmodifiableSet(rels);
    }
}
此实例化器将控制自定义序列化程序的生命周期。它维护一个内部实例
Jackson2HalModule.halhandler实例化器
,并将所有其他序列化程序委托给该实例

第5步:将所有内容放在一起:

public class HalHandlerInstantiator extends HandlerInstantiator {

    private final Jackson2HalModule.HalHandlerInstantiator halHandlerInstantiator;
    private final Map<Class<?>, JsonSerializer<?>> serializerMap = new HashMap<>();

    public HalHandlerInstantiator(RelProvider relProvider, CurieProvider curieProvider, HalMultipleLinkRels halMultipleLinkRels) {
        this(relProvider, curieProvider, halMultipleLinkRels, true);
    }

    public HalHandlerInstantiator(RelProvider relProvider, CurieProvider curieProvider, HalMultipleLinkRels halMultipleLinkRels, boolean enforceEmbeddedCollections) {
        halHandlerInstantiator = new Jackson2HalModule.HalHandlerInstantiator(relProvider, curieProvider, enforceEmbeddedCollections);

        serializerMap.put(HalLinkListSerializer.class, new HalLinkListSerializer(curieProvider, halMultipleLinkRels));
    }

    @Override
    public JsonDeserializer<?> deserializerInstance(DeserializationConfig config, Annotated annotated, Class<?> deserClass) {
        return halHandlerInstantiator.deserializerInstance(config, annotated, deserClass);
    }

    @Override
    public KeyDeserializer keyDeserializerInstance(DeserializationConfig config, Annotated annotated, Class<?> keyDeserClass) {
        return halHandlerInstantiator.keyDeserializerInstance(config, annotated, keyDeserClass);
    }

    @Override
    public JsonSerializer<?> serializerInstance(SerializationConfig config, Annotated annotated, Class<?> serClass) {
        if(serializerMap.containsKey(serClass)) {
            return serializerMap.get(serClass);
        } else {
            return halHandlerInstantiator.serializerInstance(config, annotated, serClass);
        }
    }

    @Override
    public TypeResolverBuilder<?> typeResolverBuilderInstance(MapperConfig<?> config, Annotated annotated, Class<?> builderClass) {
        return halHandlerInstantiator.typeResolverBuilderInstance(config, annotated, builderClass);
    }

    @Override
    public TypeIdResolver typeIdResolverInstance(MapperConfig<?> config, Annotated annotated, Class<?> resolverClass) {
        return halHandlerInstantiator.typeIdResolverInstance(config, annotated, resolverClass);
    }
}

我有一个解决这个问题的方法,与Chris的答案类似。主要区别在于我没有扩展
Jackson2HalModule
,而是创建了一个新的处理程序实例化器,并将其设置为我自己创建的
Jackson2HalModule
的新实例的处理程序实例化器。我希望SpringHateoas最终能够在本地支持这一功能;我有一个尝试这样做的方法。以下是我如何实施我的变通方法:

步骤1:创建mixin类:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ForceMultiLink {

    String[] value();

}
步骤3:创建新的序列化程序,该序列化程序将覆盖Spring HATEOAS的链接列表序列化程序:

public class HalMultipleLinkRels {
    private final Set<String> rels;

    public HalMultipleLinkRels(String... rels) {
        this.rels = new HashSet<String>(Arrays.asList(rels));
    }

    public Set<String> getRels() {
        return Collections.unmodifiableSet(rels);
    }
}
此实例化器将控制自定义序列化程序的生命周期。它维护一个内部实例
Jackson2HalModule.halhandler实例化器
,并将所有其他序列化程序委托给该实例

第5步:将所有内容放在一起:

public class HalHandlerInstantiator extends HandlerInstantiator {

    private final Jackson2HalModule.HalHandlerInstantiator halHandlerInstantiator;
    private final Map<Class<?>, JsonSerializer<?>> serializerMap = new HashMap<>();

    public HalHandlerInstantiator(RelProvider relProvider, CurieProvider curieProvider, HalMultipleLinkRels halMultipleLinkRels) {
        this(relProvider, curieProvider, halMultipleLinkRels, true);
    }

    public HalHandlerInstantiator(RelProvider relProvider, CurieProvider curieProvider, HalMultipleLinkRels halMultipleLinkRels, boolean enforceEmbeddedCollections) {
        halHandlerInstantiator = new Jackson2HalModule.HalHandlerInstantiator(relProvider, curieProvider, enforceEmbeddedCollections);

        serializerMap.put(HalLinkListSerializer.class, new HalLinkListSerializer(curieProvider, halMultipleLinkRels));
    }

    @Override
    public JsonDeserializer<?> deserializerInstance(DeserializationConfig config, Annotated annotated, Class<?> deserClass) {
        return halHandlerInstantiator.deserializerInstance(config, annotated, deserClass);
    }

    @Override
    public KeyDeserializer keyDeserializerInstance(DeserializationConfig config, Annotated annotated, Class<?> keyDeserClass) {
        return halHandlerInstantiator.keyDeserializerInstance(config, annotated, keyDeserClass);
    }

    @Override
    public JsonSerializer<?> serializerInstance(SerializationConfig config, Annotated annotated, Class<?> serClass) {
        if(serializerMap.containsKey(serClass)) {
            return serializerMap.get(serClass);
        } else {
            return halHandlerInstantiator.serializerInstance(config, annotated, serClass);
        }
    }

    @Override
    public TypeResolverBuilder<?> typeResolverBuilderInstance(MapperConfig<?> config, Annotated annotated, Class<?> builderClass) {
        return halHandlerInstantiator.typeResolverBuilderInstance(config, annotated, builderClass);
    }

    @Override
    public TypeIdResolver typeIdResolverInstance(MapperConfig<?> config, Annotated annotated, Class<?> resolverClass) {
        return halHandlerInstantiator.typeIdResolverInstance(config, annotated, resolverClass);
    }
}
不要忘记HAL所需的“自我”资源链接。 在这种情况下,只有一个链接并不常见。

不要忘记HAL所需的“自我”资源链接。 在这种情况下,只有一个lin并不常见