0

I have class User which is generated by wsdl. I can't change this class. I have a task in which I should save sessions in Redis instead of Tomcat. But after authentification I got an error, which said that class User must be Serializable. How can I make this class Serializable?

Zulfuqar Aliyev
  • 241
  • 3
  • 18
  • Finally I could do it via pom.xml. Here is the way I solved it https://stackoverflow.com/questions/52658580/how-to-make-java-class-serializable-which-is-generated-by-wsdl/52702679#52702679 – Zulfuqar Aliyev Oct 09 '18 at 06:07

1 Answers1

0

You have to write your own serializer for User. Assumed that your user is stored somehow in a session class and this session should be serialized:

public class TheSession implements Serializable
{
    ...
    private transient User user;
    ...
    private void writeObject(ObjectOutputStream oos) throws IOException {
        oos.defaultWriteObject(); // serializes all properties except the transients
        // serialize single fields of the User class
        oos.writeObject(user.getName());
        oos.writeInt(user.getId());
        ...
    }
    private void readObject(ObjectInputStream ois) throws  IOException, ClassNotFoundException {
        ois.defaultReadObject();
        String userName = (String)ois.readObject;
        int userId = ois.readId();
        this.user = new User(userName, userId); // assume that we have such a constructor
        ... // fill the other fields of user.
    }
    ...
}
Torsten Fehre
  • 567
  • 2
  • 7
  • Thanks for answer. I read something like this in inthernet. But I can't understand how it will help me. User class is used for example in class SoapUsernamePasswordAuthenticationToken. And there is User userInfo variable, then User getUserInfo() and setUserInfo(User userInfo) methods. And other classes use User as well. How can I use TheSession class in this situation – Zulfuqar Aliyev Oct 04 '18 at 13:19
  • Actually I found one post here. But I don't know how to use it properly https://stackoverflow.com/questions/1513972/how-to-generate-a-java-class-which-implements-serializable-interface-from-xsd-us – Zulfuqar Aliyev Oct 04 '18 at 13:20