如何用假客户端反序列化application/json+hal消息?

How to deserialize application/json+hal messages with feign client?

我正在尝试从我的 db-service 中反序列化我的 application/json+hal 消息,该消息是由 Spring.io jpa-data-rest 模块生成的,但我无法反序列化该消息作为我的业务逻辑服务的 java 模型。

我尝试根据以下教程更新我的项目:https://reflectoring.io/accessing-spring-data-rest-with-feign/

如果我通过在末尾返回字符串来使用假装客户端访问其余资源,它会成功返回我的数据库服务的字符串响应。如果我将响应作为资源返回,我会收到一个空的 java 对象。

作为测试,我还使用了来自我的 db-service 的 jpa 实体 classes 作为反序列化模板 class,returns 也是一个空的 java 对象.

反序列化使用的模型:


public abstract class AbstractNationModelBase implements Serializable {

    private String uuid;

    public AbstractNationModelBase(String uuid) {
        this.uuid = uuid;
    }


    public AbstractNationModelBase() {
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

[...]
}


public class UserModel extends AbstractNationModelBase implements Serializable {


    private String username;

    private String password;

    private boolean isAdmin;


    public UserModel(String uuid, String username, String password, boolean isAdmin) {
        super(uuid);
        this.username = username;
        this.password = password;
        this.isAdmin = isAdmin;
    }

    public UserModel(String username, String password, boolean isAdmin) {
        this.username = username;
        this.password = password;
        this.isAdmin = isAdmin;
    }

    public UserModel(){
        super();
    }


@FeignClient(value="nation-database-service")
public interface UserFeignProxy {



    @RequestMapping(method = RequestMethod.GET,value="/users")
    Resources<UserModel> GetAllUsersByModel();
}

正确的回复文字:


{
    "_embedded": {
        "users": [
            {
                "uuid": "815b53b0-cd33-4813-bce3-a7c4989b0b10",
                "username": "Testiman",
                "password": "peter_test",
                "admin": true,
                "_links": {
                    "self": {
                        "href": "http://localhost:8081/users/1"
                    },
                    "user": {
                        "href": "http://localhost:8081/users/1"
                    }
                }
            }
        ]
    },
    "_links": {
        "self": {
            "href": "http://localhost:8081/users{?page,size,sort}",
            "templated": true
        },
        "profile": {
            "href": "http://localhost:8081/profile/users"
        },
        "search": {
            "href": "http://localhost:8081/users/search"
        }
    },
    "page": {
        "size": 20,
        "totalElements": 1,
        "totalPages": 1,
        "number": 0
    }
}

结果应该是反序列化到我的模型中class。如果有人能分享一些经验,我将不胜感激。

我在检查了以下 github 问题后找到了解决方案:

https://github.com/spring-cloud/spring-cloud-openfeign/issues/127

我添加了问题中提到的自定义 AbstractJackson2HttpMessageConverter