throw java.io.NotSerializableException during deserialization (#22821)

* throw java.io.NotSerializableException during deserialization

* formatting fix

* add annotation to all fromBinary methods

* remove annotation

* add annotation to remaining methods in Serialization.scala
This commit is contained in:
Nafer Sanabria 2017-08-08 06:15:18 -05:00 committed by Patrik Nordwall
parent e4b8256009
commit ad1ffeda2b
6 changed files with 37 additions and 12 deletions

View file

@ -6,6 +6,8 @@ package jdocs.persistence;
import docs.persistence.ExampleJsonMarshaller;
import docs.persistence.proto.FlightAppModels;
import java.io.NotSerializableException;
import java.nio.charset.Charset;
import spray.json.JsObject;
@ -72,7 +74,7 @@ public class PersistenceSchemaEvolutionDocTest {
return o.getClass().getName();
}
@Override public Object fromBinary(byte[] bytes, String manifest) {
@Override public Object fromBinary(byte[] bytes, String manifest) throws NotSerializableException{
if (seatReservedManifest.equals(manifest)) {
// use generated protobuf serializer
try {
@ -81,7 +83,7 @@ public class PersistenceSchemaEvolutionDocTest {
throw new IllegalArgumentException(e.getMessage());
}
} else {
throw new IllegalArgumentException("Unable to handle manifest: " + manifest);
throw new NotSerializableException("Unable to handle manifest: " + manifest);
}
}
@ -208,13 +210,13 @@ public class PersistenceSchemaEvolutionDocTest {
}
// deserialize the object, using the manifest to indicate which logic to apply
@Override public Object fromBinary(byte[] bytes, String manifest) {
@Override public Object fromBinary(byte[] bytes, String manifest) throws NotSerializableException {
if (personManifest.equals(manifest)) {
String nameAndSurname = new String(bytes, utf8);
String[] parts = nameAndSurname.split("[|]");
return new Person(parts[0], parts[1]);
} else {
throw new IllegalArgumentException(
throw new NotSerializableException(
"Unable to deserialize from bytes, manifest was: " + manifest +
"! Bytes length: " + bytes.length);
}
@ -412,12 +414,12 @@ public class PersistenceSchemaEvolutionDocTest {
}
}
@Override public Object fromBinary(byte[] bytes, String manifest) {
@Override public Object fromBinary(byte[] bytes, String manifest) throws NotSerializableException {
if (oldPayloadClassName.equals(manifest))
return new SamplePayload(new String(bytes, utf8));
else if (myPayloadClassName.equals(manifest))
return new SamplePayload(new String(bytes, utf8));
else throw new IllegalArgumentException("unexpected manifest [" + manifest + "]");
else throw new NotSerializableException("unexpected manifest [" + manifest + "]");
}
}
//#string-serializer-handle-rename

View file

@ -4,8 +4,11 @@
package docs.persistence
import java.io.NotSerializableException
import scala.language.reflectiveCalls
import java.nio.charset.Charset
import akka.actor.ActorSystem
import akka.persistence.journal.{ EventAdapter, EventSeq }
import akka.serialization.{ SerializationExtension, SerializerWithStringManifest }
@ -13,6 +16,7 @@ import akka.testkit.TestKit
import com.typesafe.config._
import org.scalatest.WordSpec
import spray.json.JsObject
import scala.concurrent.duration._
import docs.persistence.proto.FlightAppModels
@ -82,7 +86,7 @@ class ProtobufReadOptional {
// use generated protobuf serializer
seatReserved(FlightAppModels.SeatReserved.parseFrom(bytes))
case _ =>
throw new IllegalArgumentException("Unable to handle manifest: " + manifest)
throw new NotSerializableException("Unable to handle manifest: " + manifest)
}
override def toBinary(o: AnyRef): Array[Byte] = o match {
@ -197,7 +201,7 @@ object SimplestCustomSerializer {
val nameAndSurname = new String(bytes, Utf8)
val Array(name, surname) = nameAndSurname.split("[|]")
Person(name, surname)
case _ => throw new IllegalArgumentException(
case _ => throw new NotSerializableException(
s"Unable to deserialize from bytes, manifest was: $manifest! Bytes length: " +
bytes.length)
}
@ -317,7 +321,7 @@ class RenamedEventAwareSerializer extends SerializerWithStringManifest {
manifest match {
case OldPayloadClassName => SamplePayload(new String(bytes, Utf8))
case MyPayloadClassName => SamplePayload(new String(bytes, Utf8))
case other => throw new Exception(s"unexpected manifest [$other]")
case other => throw new NotSerializableException(s"unexpected manifest [$other]")
}
}
//#string-serializer-handle-rename