我有一个名为Activity的实体类,该类一直在反序列化,工作正常,直到我决定向实体添加一个int类型的id字段。该字段的getter和setter是公共的,似乎很好,但是当我运行从客户端创建对象的测试时,我会遇到如下反序列化错误。我正在使用jersey学习REST教程。
我已经检查了这个字段的setter和getter的可见性,它们既不是包私有的,也不是私有的。
这是我得到的错误:
javax.ws.rs.ProcessingException: Error deserializing object from entity stream.
at org.glassfish.jersey.jsonb.internal.JsonBindingProvider.readFrom(JsonBindingProvider.java:77)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:233)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:212)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:132)
at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1067)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:850)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:784)
at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:297)
at org.glassfish.jersey.client.InboundJaxrsResponse$1.call(InboundJaxrsResponse.java:91)
at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
at org.glassfish.jersey.internal.Errors.process(Errors.java:205)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:365)
at org.glassfish.jersey.client.InboundJaxrsResponse.runInScopeIfPossible(InboundJaxrsResponse.java:240)
at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:88)
at tzpl.client.ActivityClient.create(ActivityClient.java:54)
at tzpl.client.ActivityClientTest.testCreate(ActivityClientTest.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: javax.json.bind.JsonbException: Can't deserialize JSON array into: class tzpl.model.Activity
at org.eclipse.yasson.internal.serializer.DeserializerBuilder.build(DeserializerBuilder.java:141)
at org.eclipse.yasson.internal.Unmarshaller.deserializeItem(Unmarshaller.java:60)
at org.eclipse.yasson.internal.Unmarshaller.deserialize(Unmarshaller.java:51)
at org.eclipse.yasson.internal.JsonBinding.deserialize(JsonBinding.java:45)
at org.eclipse.yasson.internal.JsonBinding.fromJson(JsonBinding.java:85)
at org.glassfish.jersey.jsonb.internal.JsonBindingProvider.readFrom(JsonBindingProvider.java:75)
... 38 more
Process finished with exit code 255
这是活动实体:
package tzpl.model;
import javax.html" target="_blank">xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Activity {
private int id;
private String description;
private int duration;
private boolean worthIt;
private User user;
public Activity(){
}
public Activity (int id, String description, int duration, boolean worthIt){
this.description = description;
this.duration = duration;
this.worthIt = worthIt;
this.id = id;
}
@XmlElement(name="id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement(name="user")
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@XmlElement(name="verdict")
public boolean isWorthIt() {
return worthIt;
}
public void setWorthIt(boolean worthIt) {
this.worthIt = worthIt;
}
@XmlElement(name="desc")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@XmlElement(name="time-taken")
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
}
这是我尝试用来获取活动对象的客户端;
package tzpl.client;
import tzpl.model.Activity;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
public class ActivityClient {
private Client client;
public ActivityClient() {
client = ClientBuilder.newClient();
}
public Activity get(String id) {
WebTarget target = client.target("http://localhost:8080/myapp" +
"/activities");
Response response =
target.path("activity/" + id).request(MediaType.APPLICATION_JSON).get(Response.class);
if (response.getStatus() != 200) {
throw new RuntimeException(response.getStatus() + ": An error was" +
" encountered on the server.");
}
return response.readEntity(Activity.class);
}
public List<Activity> get() {
WebTarget target = client.target("http://localhost:8080/myapp");
List<Activity> response =
target.path("activities/").request(MediaType.APPLICATION_JSON).get(List.class);
return response;
}
public Activity create(Activity activity) {
WebTarget target = client.target("http://localhost:8080/myapp" +
"/activities/");
Response response =
target.path("activity").request(MediaType.APPLICATION_JSON).post(Entity.entity(activity, MediaType.APPLICATION_JSON));
if (response.getStatus() != 200) {
throw new RuntimeException(response.getStatus() + ": there was an" +
" error on the server.");
}
return response.readEntity(Activity.class);
}
public Activity update(Activity activity) {
WebTarget target = client.target("http://localhost:8080/myapp" +
"/activities/");
Response response =
target.path("activity/"+activity.getId()).request(MediaType.APPLICATION_JSON).put(Entity.entity(activity, MediaType.APPLICATION_JSON));
if (response.getStatus() != 200) {
throw new RuntimeException(response.getStatus() + ": there was " +
"an" +
" error on the server.");
}
return response.readEntity(Activity.class);
}
}
我在我的客户端测试中运行testCreate方法的测试,如下所示:
package tzpl.client;
import org.junit.Test;
import tzpl.model.Activity;
import tzpl.repository.ActivityRepository;
import tzpl.repository.ActivityResourceStub;
import java.util.List;
import static org.junit.Assert.assertNotNull;
public class ActivityClientTest {
@Test
public void testPut(){
ActivityRepository activityRepository = new ActivityResourceStub();
Activity activity = new Activity(6,"Mountain climbing", 45, true);
Activity activity2 = activityRepository.listAllActivities().get(2);
ActivityClient client = new ActivityClient();
activity2 = client.update(activity2);
assertNotNull(activity2);
}
@Test
public void testGet(){
ActivityClient client = new ActivityClient();
Activity activity = client.get("1");
System.out.println(activity.getDescription());
assertNotNull(activity);
}
@Test
public void testGetList(){
ActivityClient client = new ActivityClient();
List<Activity> activities = client.get();
System.out.println(activities);
assertNotNull(activities);
}
@Test (expected=RuntimeException.class)
public void testGetWithBadRequest(){
ActivityClient client = new ActivityClient();
client.get("");
}
@Test(expected=RuntimeException.class)
public void testGetWithNotFound(){
ActivityClient client = new ActivityClient();
client.get("777");
}
@Test
public void testCreate(){
ActivityClient client = new ActivityClient();
Activity activity = new Activity(7,"Skiing", 20, false);
activity = client.create(activity);
assertNotNull(activity);
}
}
在我断言活动为空或缺少活动之前,我从activity客户端收到一个错误,抱怨它无法反序列化传入的Jason流。我在pom.xml中有以下依赖项。很明显,我对使用jason有依赖性,如下面的pom所示,正如我所提到的,只有在引入id字段之后,测试才会中断。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tzpl</groupId>
<artifactId>com.tzpl</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>com.tzpl</name>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<!-- uncomment this to get JSON support:-->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>tzpl.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<jersey.version>2.28</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
我需要世贸组织如何克服这种反序列化错误的帮助。
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-moxy</artifactId>
</dependency>
将依赖项更改为此。它会正常工作。
根本原因很清楚:
无法将JSON数组反序列化为:类tzpl.model.Activity
反序列化程序看到接下来出现的是一个数组,但您说它应该是一个Activity对象,所以它不能工作。
为了确保在我的RESTful web服务中来回发送的数据不冗余,每个嵌套对象都只序列化了它的ID(消息的用户只序列化了用户ID,因为客户端和服务器都已经知道用户的所有详细信息)。 序列化工作正常,产生以下效果: 问题:反序列化不会生成仅具有其ID的嵌套对象。生成的反序列化嵌套对象为空。 以下是前面提到的消息和用户对象。序列化“策略”是从此处指定的第三个选项中使用的:如何仅序列化Jackson的子级
我尝试反序列化一个巨大的API负载。这个有效负载包含的字段比我需要的要多,因此我正在使用。但是,在某些情况下,反序列化会失败,并显示错误消息: 我找到了解决方案,建议使用 我试过这个。但这于事无补。此外,我的结果数据不是一个单一的值数组。它实际上包含两个值——因此解决方案无论如何都不会相加。 以下是反序列化的目标类。 下面是我测试它的单元测试: 这是我想要反序列化的有效负载
我的项目是关于图书馆管理系统的。我必须将数据存储到类的对象中,然后将它们存储到ArrayList中。然后我在序列化数据。 我有这些功能: null 我用来反序列化的代码: 我得到了一个异常,因为BufferedReader需要是瞬态的,这是修复的。但现在,我认为我的序列化不起作用了。每次我创建一本书,我都将它的索引(对象编号)存储在一个文本文件中。我的搜索技术都不起作用:ArrayIndexOut
错误: java.lang.ClassNotFoundException:testprocedure.tp$3在java.net.URLClassLoader$1上运行(未知源)在java.net.URLClassLoader上运行(未知源)在java.security.accessController.doprivileged(本机方法)在java.net.URLClassLoader.find
我已经编写了一个自定义反序列化器,它将接受字符串并对其进行反序列化公共静态类MyDeserializer extends JsonDeserializer{
问题内容: 你好亲爱的同事们, 我有一个Garden类,在其中我可以序列化和反序列化多个Plant类对象。如果想将其分配给mein静态方法中的调用变量,则可以进行序列化,但是不能进行反序列化。 反序列化代码: 我的调用代码: 编辑 我有一个空的Pointer异常 @NilsH的解决方案工作正常,谢谢! 问题答案: 如何序列化整个列表呢?无需序列化列表中的每个对象。 如果那不能解决您的问题,请发布有