【发布时间】: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