【问题标题】:Using Spring/ Hibernate /Jackson. Getting error -- Could not read JSON: Could not initialize class exception使用 Spring/休眠/杰克逊。出现错误——无法读取 JSON:无法初始化类异常
【发布时间】:2013-12-16 10:45:32
【问题描述】:

我们在应用程序中使用了 Jackson、spring 和 hibernate 的组合。 下面我列出的代码工作正常

控制器

 @RequestMapping(value = ServiceEndpoints.MyService, method = RequestMethod.POST)
@ResponseBody   
public MyResponse updateEntity(final HttpServletRequest request, @RequestBody final MyEntity myEntity )  {
//Service Call  
}

实体

 @Entity
 public class MyEntity extends BaseEntity implements Serializable {
    @Column
private String metaTags;

    public String getMetaTags() {
    return metaTags;
}

public void setMetaTags(String metaTags) {          
    this.metaTags = metaTags;
}

servlet-contect.xml

        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <bean id="mappingJacksonHttpMessageConverter"
                   class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                <property name="objectMapper">
                    <ref bean="jacksonObjectMapper" />
                </property>
            </bean>
        </list>
    </property>
</bean>

上面的代码工作正常。我们从 UI 获取数据,它从 JSON 转换为实体,我们能够保存相同的数据。 但后来我添加了一个代码来清除实体字段中的一些字符,如下所示

   public void setMetaTags(String comments) {       
    this.metaTags = MyClassWithStaticMethods.OnesuchStaticMethod(metaTags); 

}

我得到以下异常

Could not read JSON: Could not initialize class MyClassWithStaticMethods (through reference chain: MyEntity["metaTags"]); 
 nested exception is org.codehaus.jackson.map.JsonMappingException: Could not initialize class MyClassWithStaticMethods (through reference chain: MyEntity["metaTags"])

现在我确实浏览了一些类似这样的链接 http://cowtowncoder.com/blog/archives/2010/08/entry_411.html

但此链接与我的问题并不真正相关。 有没有人可以帮忙

【问题讨论】:

  • public void setMetaTags(String comments) { this.metaTags = MyClassWithStaticMethods.OnesuchStaticMethod(metaTags); } 这可能会使其成为错误的 JSON,并且 JSON 无法解析它。您可以尝试在此方法调用之前和之后查看 JSON 的文本表示。
  • 问题出在 MyClassWithStaticMethods 类中。你能把这节课的内容放进去吗?

标签: java json spring hibernate


【解决方案1】:

问题是“MyClassWithStaticMethods”又使用了同一个 jar 的多个版本(感谢 @Abhijith Nagarajan 要求调查它)。

我的方法是使用 guava API,而应用程序的另一部分使用的是旧的 google-collections。这两个人造成了问题。

问候

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-20
    • 2015-02-07
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2019-04-16
    相关资源
    最近更新 更多