简体   繁体   English

InetSocketAddress的Spring数据mongodb序列化

[英]Spring data mongodb serialization of InetSocketAddress

First off: I am very new to Spring, therefore I am sorry if I fail to provide all information necessary for the question.首先:我对 Spring 非常陌生,因此如果我未能提供问题所需的所有信息,我深表歉意。 My problem is as follows: I have the following class which objects I want to save in a mongoDB我的问题如下:我有以下类,我想在 mongoDB 中保存哪些对象

public class Subscription implements Serializable {

    private String type;
    private InetSocketAddress host;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public InetSocketAddress getHost() {
        return host;
    }

    public void setHost(InetSocketAddress host) {
        this.host = host;
    }


    public Subscription(){}

I do this by just defining a Repository interface and autowiring it into my application (which works fine for another repository)我通过定义一个 Repository 接口并将其自动装配到我的应用程序中来做到这一点(这对另一个存储库工作正常)

public interface SubscriptionRepository extends MongoRepository<Subscription, String> {
}

I can save Subscription objects into the repository, but reading them into a List<Subscription> via SubscriptionRepository.findall() gives me an error我可以将订阅对象保存到存储库中,但是通过SubscriptionRepository.findall()将它们读入List<Subscription>给我一个错误

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.net.InetSocketAddress]: No default constructor found; nested exception is java.lang.NoSuchMethodException: java.net.InetSocketAddress.<init>()

Looking into the database the InetSocketAddress object is saved kind of weird查看数据库 InetSocketAddress 对象保存的有点奇怪

{ "_id" : ObjectId("5e1a48f4a6e30c7d2089e5cd"), "type" : "test", "host" : { "holder" : { "addr" : { "holder" : { "address" : 174417169, "family" : 1 }, "_class" : "java.net.Inet4Address" }, "port" : 0 } }, "_class" : "com.example.myproject.Subscription" }

What do I have to change in order to save the InetSocketAddress field in a way so that I can retrieve the Subscription Object from the database correctly?我必须更改什么才能以某种方式保存 InetSocketAddress 字段,以便我可以正确地从数据库中检索订阅对象?

Thank you in Advance先感谢您

InetSocketAddress is either a String host name or an InetAddress with an int port. InetSocketAddress 是字符串主机名或带有 int 端口的 InetAddress。

An InetAddress is basically an array of bytes. InetAddress 基本上是一个字节数组。

Neither InetSocketAddress nor InetAddress can be used as java beans. InetSocketAddress 和 InetAddress 都不能用作 java bean。

Instead of storing InetSocketAddress, store the String, byte[] and the port.不是存储 InetSocketAddress,而是存储 String、byte[] 和端口。 Better yet, convert the byte[] to a String representation of an IP address, and store just a String and a port, the String being either the host name or the IP address as a string.更好的是,将 byte[] 转换为 IP 地址的字符串表示,并仅存储一个字符串和一个端口,字符串是主机名或 IP 地址作为字符串。 Then add a method that constructs an InetSocketAddress whenever you need it.然后添加一个在需要时构造 InetSocketAddress 的方法。 Also add setters and getters for the port and the String host/address.还要为端口和字符串主机/地址添加 setter 和 getter。

public class Subscription implements Serializable {

    private String type;

    // instead of InetSocketAddress
    private String host;
    private int port;

    public InetSocketAddress getSocketAddress() {
            return new InetSocketAddress(host, port);
    }

    // setters and getters

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM