Spring-data-rest 在 hal+json 和普通 json 之间切换 [英] Spring-data-rest switch between hal+json and plain json

查看:93
本文介绍了Spring-data-rest 在 hal+json 和普通 json 之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 defaultMediaType 配置属性设置为application/json"以获得正常"的 JSON,这更容易处理,也是我所需要的:

I've been using the defaultMediaType config property set to "application/json" to get "normal" JSON, which is easier to handle and all I need:

{
    "links": [
    ],
    "content": [
        {
            "username": "admin",
            "id": 1,
            "authorities": [
                "ROLE_ADMIN"
            ],
            "content": [ ],
            "links": [
                {
                    "rel": "self",
                    "href": "http://localhost:8080/apiv1/data/users/1"
                },
                {
                    "rel": "logisUser",
                    "href": "http://localhost:8080/apiv1/data/users/1"
                },
                {
                    "rel": "mandant",
                    "href": "http://localhost:8080/apiv1/data/users/1/mandant"
                }
            ]
        },...
    ],
    "page": {
        "size": 20,
        "totalElements": 3,
        "totalPages": 1,
        "number": 0
    }
}

但结果是无法使用在集成测试中(不能用 TestRestTemplate 解组,也不能用 Traverson 等).

But it turned out to be impossible to use in Integration tests (can't unmarshall with TestRestTemplate, nor with Traverson, etc.).

使用 hal+json 可以很好地与 Traverson API 配合使用.

Using hal+json works nicely with the Traverson API.

现在,由于名称是 defaultMediaType 并且 Spring Data Rest Reference 总是提到支持的媒体类型",我希望使用Accept"标头在两个变体之间切换.但这不起作用(也尝试了Content-Type"和两者.使用application/json 和 application/hal+json").

Now, since the name is defaultMediaType and the Spring Data Rest Reference always mentions "Supported media types", I expected to switch between the two variants using the "Accept" header. But that doesn't work (also tried "Content-Type" and both. With "application/json and application/hal+json").

似乎还有一个属性useHalAsDefaultJsonMediaType",但据我所知,它根本没有任何作用.

There also seems to be a property "useHalAsDefaultJsonMediaType", but that doesn't do anything at all, as far as I can see.

那么所有希望都破灭了吗?为什么文档如此混乱?有一种在表示之间切换的方法,它的行为与名称所暗示的不同.没有记录的方法可以反序列化旧"表示,并且新表示不能与 TestRestTemplate 一起使用.非常,非常令人沮丧.我已经花了很多时间在这上面:(

So is all hope lost? And why is the documentation so confusing? There is one way to switch between the representations, which doesn't behave as the name suggests. There's no documented way to deserialize the "old" representation and the new one can't be used with TestRestTemplate. Very, very frustrating. I've spent lots of time on this already :(

推荐答案

制作 RepositoryRestConfigurer

Make a RepositoryRestConfigurer

@Configuration
public class RestRepositoryConfig extends RepositoryRestConfigurerAdapter {
    @Value("${spring.hateoas.use-hal-as-default-json-media-type:true}")
    private boolean useHalAsDefaultJsonMediaType;

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
         config.useHalAsDefaultJsonMediaType(useHalAsDefaultJsonMediaType);
    }
}

并设置

spring.hateoas.use-hal-as-default-json-media-type=false

这篇关于Spring-data-rest 在 hal+json 和普通 json 之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆