SpringDataJPA中No serializer

This link: https://blog.csdn.net/qq_42136250/article/details/88581844

After SpringMVC and Jpa integration, there is this problem occurs when lazy loading
the wrong reasons:
JPA lazy load the object itself as filling some of the properties, ( "hibernateLazyInitializer", "handler ", "fieldHandler"),
these properties will affect the SpringMVC return Json (because there is introspection mechanism to return,
because you need to serialize object has a property is a class type, and you use lazy loading Hibernate so here is a Hibernate proxy object the proxy object some properties can not be serialized so of error.
solution
Solution one: Add comment (but with the increase in warehouse class, the workload will increase) is added on the property

 

 

@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"})

 Solution two: Rewrite: ObjectMapper, and then configure the mapping applicationContext-mvc.xml (this method once and for all, after integration in the Spring for JPA lazy loading, that they will avoid errors No serializer)

package cn.jiedada.aisell.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JsonMapper extends ObjectMapper {
    public JsonMapper() {
        this.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        // 设置 SerializationFeature.FAIL_ON_EMPTY_BEANS 为 false
        this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    }
}
View Code

In the configuration applicationContext-mvc.xml

<MVC: Annotation-Driven> 
        <MVC: Message-Converters> 
            <the bean class = "org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 
                <Property name = "supportedMediaTypes"> 
                    <List> 
                        <value> file application / JSON; . 8 = UTF-charset </ value> 
                        <value> file application / X-WWW-form-urlencoded; charset = UTF-. 8 </ value> 
                    </ List> 
                </ Property> 
                <- Serializer No:! us configured objectMapper custom CustomMapper the extended address the subject matter of the return of the object being given issue -> 
                <Property name = "ObjectMapper">
                    <bean class="cn.jiedada.aisell.common.JsonMapper"></bean>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
View Code

 

Guess you like

Origin www.cnblogs.com/xiaoruirui/p/11669569.html