我傳遞JPA實體,稱爲檔案從我的服務器到客戶端使用GSON如何通過JPON實體與GSON?
@Entity
public class Profile implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
.
.
.
}
的GSON代碼:
Gson gson = new Gson();
Profile profile = gson.fromJson(json, Profile.class);
但我發現了:
java.lang.NoClassDefFoundError: javax.persistence.Id
java.lang.reflect.Field.getDeclaredAnnotations(Native Method)
java.lang.reflect.Field.getDeclaredAnnotations(Field.java:188)
java.lang.reflect.AccessibleObject.getAnnotations(AccessibleObject.java:188)
java.lang.reflect.AccessibleObject.getAnnotation(AccessibleObject.java:196)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldName(ReflectiveTypeAdapt erFactory.java:60)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
com.google.gson.Gson.getAdapter(Gson.java:353)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:82)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
com.google.gson.Gson.getAdapter(Gson.java:353)
com.google.gson.Gson.fromJson(Gson.java:754)
com.google.gson.Gson.fromJson(Gson.java:721)
com.google.gson.Gson.fromJson(Gson.java:670)
com.google.gson.Gson.fromJson(Gson.java:642)
我需要做什麼來解決這個問題?
看起來像一個classpath的問題給我。您的序列化邏輯是否與您的實體位於不同的存檔中? – Perception 2012-04-24 17:58:36
當然。我將Entity從客戶端傳遞到服務器,所以我需要把它放在某個地方。配置文件實體在服務器中,但我有從客戶端到服務器的claspasth。 – Rami 2012-04-24 18:04:47
它看起來很奇怪,因爲未知的註解應該被默默地忽略。你使用哪種JRE? – axtavt 2012-04-24 18:25:13