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

问题描述 投票:0回答:1

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

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

如果我通过在结尾处返回字符串来访问feign客户端的其余资源,那么它很可能会返回我的db-service的字符串响应。如果我将响应作为Resources返回,我会收到一个空的java对象。

作为测试,我还使用我的db-service中的jpa实体类作为反序列化模板类,它还返回一个空的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
    }
}

结果应该是将其反序列化为我的模型类。如果有人可以分享一些经验,我将不胜感激。

java spring spring-data-jpa spring-data-rest
1个回答
0
投票

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

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

我在问题中提到了一个自定义的AbstractJackson2HttpMessageConverter

© www.soinside.com 2019 - 2024. All rights reserved.