Solucionado (ver solução)
Solucionado
(ver solução)
4
respostas

String cannot cast to Document

Ao clicar no codigo pesquisar, recebo este erro de cast apontando para uma linha que está exatamente igual do professor. segue a linha e o erro:

Document curso = (Document)document.get("curso");

java.lang.ClassCastException: java.lang.String cannot be cast to org.bson.Document
    at com.example.monguito.codecs.AlunoCodec.decode(AlunoCodec.java:51) ~[classes/:na]
    at com.example.monguito.codecs.AlunoCodec.decode(AlunoCodec.java:18) ~[classes/:na]
    at com.mongodb.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:52) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:60) ~[mongodb-driver-core-3.8.2.jar:na]
    at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84) ~[bson-3.8.2.jar:na]
    at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41) ~[bson-3.8.2.jar:na]
    at org.bson.codecs.configuration.LazyCodec.decode(LazyCodec.java:47) ~[bson-3.8.2.jar:na]
    at org.bson.codecs.BsonDocumentCodec.readValue(BsonDocumentCodec.java:101) ~[bson-3.8.2.jar:na]
    at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:63) ~[mongodb-driver-core-3.8.2.jar:na]
    at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84) ~[bson-3.8.2.jar:na]
    at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41) ~[bson-3.8.2.jar:na]
    at com.mongodb.internal.connection.ReplyMessage.<init>(ReplyMessage.java:48) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnection.getCommandResult(InternalStreamConnection.java:393) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:299) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:99) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:444) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:72) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:200) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:269) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:131) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:123) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:242) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:233) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:136) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.operation.FindOperation$1.call(FindOperation.java:701) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.operation.FindOperation$1.call(FindOperation.java:695) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:462) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:406) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.operation.FindOperation.execute(FindOperation.java:695) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.operation.FindOperation.execute(FindOperation.java:83) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:179) ~[mongodb-driver-3.8.2.jar:na]
    at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:132) 
varias outras linhas continuando
4 respostas

O problema é no seu AlunoCodec.java

Você criou o método conforme abaixo?

public void encode(BsonWriter writer, Aluno aluno, EncoderContext encoder) {
  ObjectId id = aluno.getId();
  String nome = aluno.getNome();
  Date dataNascimento = aluno.getDataNascimento();
  Curso curso = aluno.getCurso();

  Document documento = new Document();
  documento.put("_id", id);
  documento.put("nome", nome);
  documento.put("data_nascimento", dataNascimento);
  documento.put("curso", new Document("nome", curso.getNome()));

}

Porta aqui o código, por favor

Estou na aula 2, não sei se tinha esquecido mas ao invez do new Document estava apenas com o curso.getNome(), porém mesmo apos a alteração ganho o mesmo erro. Segue meu código e o erro

@Override
    public void encode(BsonWriter bsonWriter, Aluno aluno, EncoderContext encoderContext) {
        ObjectId id = aluno.getId();
        String nome = aluno.getNome();
        Date dataNascimento = aluno.getDataNascimento();
        Curso curso = aluno.getCurso();

        Document document = new Document();

        document.put("_id", id);
        document.put("nome", nome);
        document.put("data_nascimento", dataNascimento);
        document.put("curso", new Document("nome", curso.getNome()));

        codec.encode(bsonWriter, document, encoderContext);
    }
java.lang.ClassCastException: java.lang.String cannot be cast to org.bson.Document
    at com.example.monguito.codecs.AlunoCodec.decode(AlunoCodec.java:51) ~[classes/:na]
    at com.example.monguito.codecs.AlunoCodec.decode(AlunoCodec.java:18) ~[classes/:na]
    at com.mongodb.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:52) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:60) ~[mongodb-driver-core-3.8.2.jar:na]
    at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84) ~[bson-3.8.2.jar:na]
    at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41) ~[bson-3.8.2.jar:na]
    muito erros por aqui...
    at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:299) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:99) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:444) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:72) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:200) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:269) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:131) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:123) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:242) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:233) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:136) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.operation.FindOperation$1.call(FindOperation.java:701) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.operation.FindOperation$1.call(FindOperation.java:695) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:462) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:406) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.operation.FindOperation.execute(FindOperation.java:695) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.operation.FindOperation.execute(FindOperation.java:83) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:179) ~[mongodb-driver-3.8.2.jar:na]
    at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:132) ~[mongodb-driver-3.8.2.jar:na]
    at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:86) ~[mongodb-driver-3.8.2.jar:na]
    at com.example.monguito.repositorys.AlunoRepository.obterTodosAlunos(AlunoRepository.java:48) ~[classes/:na]
    at com.example.monguito.controllers.AlunoController.listar(AlunoController.java:34) ~[classes/:na]
    continua mais codigos como os acima...
solução!

Resolvi o problema, acho que devia estar dando conflito pois outros objetos do banco test estavam como String e não Document, dando o erro. Para resolver eu deletei o database test, iniciei novamente a aplicação e cadastrei um novo aluno. seque os comandos que usei e o link de onde tirei (estou usando Windows 10):

mongo                <-- usar mongo
show dbs                <--mostrar os bancos
use test                <--selecionar o banco test da lista
db.dropDatabase        <--deletar ele
1                        <--digitar 1 para confirmar

https://www.tutorialkart.com/mongodb/mongodb-delete-database/

Perfeito Henrique, bons estudos!