javadatabaseserializationdeserializationrowmapper

Java: Deserializing Objects from Database Table Throws StreamCorruptedException


I'm working on deserializing objects stored in a database table. When attempting to read the serialized data using ObjectInputStream, I encounter a StreamCorruptedException with the message 'invalid stream header: EFBFBDEF'. The data is stored in a column named 'SERIALIZATION' in the 'OAUTH_CLIENTS' table. How can I properly deserialize the data retrieved from the database?

Here is my ClassicClientsService:

@Component
public class ClassicClientsService implements ClientDetailsService {

    private final JdbcTemplate dataTemplate;
    private final ObjectMapper objectMapper = new ObjectMapper();

    public ClassicClientsService(DataSource dataSource) {
        this.dataTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws OAuth2Exception {

        try {
            ClientDetails details = dataTemplate.queryForObject("SELECT SERIALIZATION FROM OAUTH_CLIENTS WHERE CLIENTID = ?", new ClientDetailsMapper(), new Object[]{clientId});
            return details;
        } catch (EmptyResultDataAccessException ers) {
            throw new EmptyResultDataAccessException("Client " + clientId + " was not found", 1);
        }

    }

    private class ClientDetailsMapper implements RowMapper<ClientDetails> {

        @SneakyThrows
        @Override
        public ClientDetails mapRow(ResultSet rs, int rowNum) {
            byte[] temp = rs.getBytes("SERIALIZATION");
            
            return (ClientDetails) new ObjectInputStream(new ByteArrayInputStream(temp)).readObject();
        }

    }}

public boolean addClient(ClientDetails details) throws DataAccessException {
        ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
        try {
            ObjectOutputStream outObject = new ObjectOutputStream(outBytes);
            outObject.writeObject(details);
            outObject.flush();
            outObject.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] szDetails = outBytes.toByteArray();
        dataTemplate.update("INSERT INTO OAUTH_CLIENTS (CLIENTID, PROTOCOL, SERIALIZATION) VALUES(?,1,?)", new Object[]{details.getClientId(), szDetails});
        return false;
    }

When i call the method "loadClientByCLientId" i am getting my Exception

@Configuration
@ComponentScan
public class Application {

    public static void main(String[] args) {
        migrateClients(args[0]);
    }

    private static void migrateClients(String clientId) {
        if (clientId.isEmpty()) {
            throw new RuntimeException("client id should be set as program argument");
        }
        ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
        ClassicClientsService clientDetailsService = context.getBean(ClassicClientsService.class);
        OauthClientsManager oauthClientsManager = context.getBean(OauthClientsManager.class);
        clientDetailsService.loadClientByClientId(clientId);

Logs:


    NFO: Loaded JDBC driver: com.mysql.jdbc.Driver
Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: EFBFBDEF
    at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:935)
    at java.base/java.io.ObjectInputStream.<init>(ObjectInputStream.java:374)

Solution

  • To resolve the issue, I created a new application(which does not make much sense) within a completely new project, and the data was successfully deserialized in this new environment.