自定义转换器未称为spring data mongodb [无法序列化类错误]

时间:2019-03-06 10:45:54

标签: java spring mongodb spring-data-mongodb converters

我有一个 User Bean,其属性为 address 。我已经为User和Address类实现了自定义转换器,并在spring XML中对其进行了配置。每次保存User对象时,都会调用User的转换器。但是,保存用户时不会调用Address的转换器,并且抛出无法序列化com.mkyong.model.Address 类。

这是我的类结构和Xml配置:

用户类别:

package com.mkyong.model;


import java.util.Map;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "person")
public class User {

    @Id
    private String id;

    String username;

    String password;

    Address address;

    Map data;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Map getData() {
        return data;
    }

    public void setData(Map data) {
        this.data = data;
    }

    public User(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
    }

}

地址类别:

package com.mkyong.model;

import java.io.Serializable;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "address")
public class Address implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 2310280839505243479L;
    String city;
    String dist;
    String state;

    public Address(String city, String dist, String state) {
        super();
        this.city = city;
        this.dist = dist;
        this.state = state;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getDist() {
        return dist;
    }

    public void setDist(String dist) {
        this.dist = dist;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    @Override
    public String toString() {
        return "Address [city=" + city + ", dist=" + dist + ", state=" +    state + "]";
    }
}

弹簧配置:

<mongo:mongo host="127.0.0.1" port="27017" />
    <mongo:db-factory dbname="springbootdb" />

    <mongo:mapping-converter id="mongoConverter">
        <mongo:custom-converters base-package="com.mkyong.model">
            <mongo:converter>
                <bean class="com.mkyong.converter.UserConverter" />
            </mongo:converter>
            <mongo:converter>
                <bean class="com.mkyong.converter.AddressConverter" />
            </mongo:converter>
        </mongo:custom-converters>
    </mongo:mapping-converter>

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoConverter" ref="mongoConverter" />
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    </bean>

用户转换器:

package com.mkyong.converter;

import org.springframework.core.convert.converter.Converter;
import com.mkyong.model.User;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

public class UserConverter implements Converter<User, DBObject> {

    @Override
    public DBObject convert(User user) {
        DBObject dbObject = new BasicDBObject();
        dbObject.put("_id", user.getId());
        dbObject.put("_address", user.getAddress());
        System.out.println("called !!!!!");
        return dbObject;
    }

}

地址转换器:

package com.mkyong.converter;

import org.springframework.core.convert.converter.Converter;
import com.mkyong.model.Address;
import com.mkyong.model.DecimalNumber;
import com.mkyong.model.User;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

public class AddressConverter implements Converter<Address, String> {

    @Override
    public String convert(Address user) {
        System.out.println("Not called!!!");
        return user==null ? null : user.toString();
    }

}

我对春季mongodb非常陌生。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

请用$search_term = $this->input->post('search'); 注释$search_term = (isset($this->input->post('search')) && !empty($this->input->post('search'))) ? $this->input->post('search') : ''; ,以便Spring Container可以选择您的转换器。