オブジェクトの完全一致/反復のシリアル化中に回避する方法はありますか?たとえば、次のようにクライアント上Javaのシリアル化オブジェクト
-
com.example.myProjectOne.model.MyClass
サーバー側 -
com.example.notMyProject.entity 。マイクラス
私は -
にjava.lang.ClassNotFoundException:すべてのワークパッケージの名前の完全な偶然の一致と com.example.myProjectOne.model.MyClass
public class Server implements Runnable {
private SettingsConnection settingsConnection;
private OnReceiveObject onReceiveObject;
private Serializer serializer;
/**
* @param remoteServerAddress - address remote server
* @param inputPort - the port on which the is server
* @param outputPort - the port which used for send
* @param password - the password which should to be same on the client side and the server side
* @param handler - defines the name of the method, which should to be called, after received the data on server side
*/
public Server(String remoteServerAddress, int inputPort, int outputPort, String password, OnReceiveObject handler) {
settingsConnection = new SettingsConnection();
settingsConnection.setAddressRemoteServer(remoteServerAddress);
settingsConnection.setInputPort(inputPort);
settingsConnection.setOutputPort(outputPort);
this.onReceiveObject = handler;
serializer = new Serializer();
new Thread(this).start();
}
public void sendData(Serializable object, String callBackFunction) {
Container container = new Container();
try {
container.setData(serializer.serialize(object), container.getInitVector()));
container.setHandler(callBackFunction);
InetAddress ipAddress = InetAddress.getByName(settingsConnection.getAddressRemoteServer());
try (Socket socketConnectionToSever = new Socket(ipAddress, settingsConnection.getOutputPort())) {
OutputStream outputStream = socketConnectionToSever.getOutputStream();
if (outputStream != null) {
outputStream.write(serializer.serialize(container));
outputStream.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
final ExecutorService asyncTakesCode = Executors.newCachedThreadPool();
Runnable threadTaskServer = new Runnable() {
@Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(settingsConnection.getInputPort());
while (true) {
Socket connectionSocketClient = serverSocket.accept();
asyncTakesCode.submit(new ThreadTaskClient(connectionSocketClient));
}
} catch (IOException e) {
System.err.println("Unable to process client request");
e.printStackTrace();
}
}
};
Thread threadServer = new Thread(threadTaskServer);
threadServer.start();
}
private class ThreadTaskClient implements Runnable {
private final Socket connectionSocketClient;
private ThreadTaskClient(Socket connectionSocketClient) {
this.connectionSocketClient = connectionSocketClient;
}
@Override
public void run() {
InputStream inputStream = null;
try {
inputStream = connectionSocketClient.getInputStream();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
Object o = serializer.deserialize(IOUtils.readFully(inputStream, -1, false));
if (o instanceof Container) {
Container container = (Container) o;
Serializable remoteObject = (Serializable) serializer.deserialize(container.getData()));
String callBackFunction = container.getHandler();
onReceiveObject.processRemoteObject(remoteObject, callBackFunction);
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
connectionSocketClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
はあなたのコードを示しています。 – DimaSan
*どのように*シリアル化されているかを制御できますか? – Bohemian
注釈@Entityなどを使用することを忘れないでください。 –