从 mysql 数据库获取 JSONArray 使用 Spring 引导 JPA
Getting JSONArray from mysql database Using Spring boot JPA
在我的 mysql table 中有存储 'JSONArray'
的列
这是 spring-boot 项目中模型 class 的一部分。
public class SubQuestions implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "sub_questionId", nullable = false,columnDefinition = "INT(11) UNSIGNED")
private Integer sub_questionId;
private JSONArray answers;
}
已经模型 class 具有空构造函数、具有所有字段的构造函数、getter 和 setter。
这是我的 SubQuestionsRepository 界面。
public interface SubQuestionsRepository extends Serializable,JpaRepository<Questions,Integer>{
}
这是我的控制器的一部分 class。
public class SubQuestionsController implements Serializable{
private SubQuestionsRepository subquestionsrepository;
public SubQuestionsController(SubQuestionsRepository subquestionsrepository) {
super();
this.SubQuestionsRepository = subquestionsrepository;
}
@GetMapping("/getall")
public Collection<SubQuestions> getallnestedques(){
return subquestionsrepository.getactiveques();
}
}
但是当我调用“getallnestedques()”方法时出现以下错误。
There was an unexpected error (type=Internal Server Error, status=500).
could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize
org.springframework.orm.jpa.JpaSystemException: could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:353)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:528)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMeth
我如何解决这个问题?
你能试试下面的解决方案吗?
需要将 answers
列声明为 Lob
,如下例所示:
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import org.json.JSONArray;
@Entity
public class SubQuestions implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "sub_questionId", nullable = false,columnDefinition = "INT(11) UNSIGNED")
private Integer sub_questionId;
@Lob
@Column
@Convert(converter = JSONArrayConverter.class)
private JSONArray answers;
}
属性转换器 JSONArrayConverter
在存储到数据库之前将 JSONArray
对象转换为 String
并在从数据库读取值后转换为 JSONArray
:
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import org.json.JSONArray;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Logger;
@Converter(autoApply = true)
public class JSONArrayConverter implements AttributeConverter<JSONArray, String> {
private static final Logger logger = (Logger) LoggerFactory.getLogger(JSONArrayConverter.class);
@Override
public String convertToDatabaseColumn(JSONArray array)
{
String data = null;
try
{
data = array.toString();
}
catch (final Exception e)
{
logger.error("JSON writing error", e);
}
return data;
}
@Override
public JSONArray convertToEntityAttribute(String data)
{
JSONArray array = null;
try
{
array = new JSONArray(data);
}
catch (final Exception e)
{
logger.error("JSON reading error", e);
}
return array;
}
}
在我的 mysql table 中有存储 'JSONArray'
的列这是 spring-boot 项目中模型 class 的一部分。
public class SubQuestions implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "sub_questionId", nullable = false,columnDefinition = "INT(11) UNSIGNED")
private Integer sub_questionId;
private JSONArray answers;
}
已经模型 class 具有空构造函数、具有所有字段的构造函数、getter 和 setter。
这是我的 SubQuestionsRepository 界面。
public interface SubQuestionsRepository extends Serializable,JpaRepository<Questions,Integer>{
}
这是我的控制器的一部分 class。
public class SubQuestionsController implements Serializable{
private SubQuestionsRepository subquestionsrepository;
public SubQuestionsController(SubQuestionsRepository subquestionsrepository) {
super();
this.SubQuestionsRepository = subquestionsrepository;
}
@GetMapping("/getall")
public Collection<SubQuestions> getallnestedques(){
return subquestionsrepository.getactiveques();
}
}
但是当我调用“getallnestedques()”方法时出现以下错误。
There was an unexpected error (type=Internal Server Error, status=500).
could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize
org.springframework.orm.jpa.JpaSystemException: could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:353)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:528)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMeth
我如何解决这个问题?
你能试试下面的解决方案吗?
需要将 answers
列声明为 Lob
,如下例所示:
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import org.json.JSONArray;
@Entity
public class SubQuestions implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "sub_questionId", nullable = false,columnDefinition = "INT(11) UNSIGNED")
private Integer sub_questionId;
@Lob
@Column
@Convert(converter = JSONArrayConverter.class)
private JSONArray answers;
}
属性转换器 JSONArrayConverter
在存储到数据库之前将 JSONArray
对象转换为 String
并在从数据库读取值后转换为 JSONArray
:
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import org.json.JSONArray;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Logger;
@Converter(autoApply = true)
public class JSONArrayConverter implements AttributeConverter<JSONArray, String> {
private static final Logger logger = (Logger) LoggerFactory.getLogger(JSONArrayConverter.class);
@Override
public String convertToDatabaseColumn(JSONArray array)
{
String data = null;
try
{
data = array.toString();
}
catch (final Exception e)
{
logger.error("JSON writing error", e);
}
return data;
}
@Override
public JSONArray convertToEntityAttribute(String data)
{
JSONArray array = null;
try
{
array = new JSONArray(data);
}
catch (final Exception e)
{
logger.error("JSON reading error", e);
}
return array;
}
}