Type Mismatch: cannot Convert from Unmarshaller to Pool.Unmarshaller

Asked

Viewed 118 times

0

I’m getting this eclipse error message in a Rest project

java client code.

package cliente;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import com.sun.xml.internal.ws.util.Pool.Unmarshaller;

import domain.Banda;
import domain.Musica;

public class Cliente {

    private static int HTTP_COD_SUCESSO = 200;

    public static void main(String[] args) throws JAXBException {

        try {

            URL url = new URL("http://localhost:8080/musicApp/banda/get");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            if (con.getResponseCode() != HTTP_COD_SUCESSO) {
                throw new RuntimeException("HTTP error code : "+ con.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader((con.getInputStream())));

            JAXBContext jaxbContext = JAXBContext.newInstance(Banda.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            Banda banda = (Banda) jaxbUnmarshaller.unmarshal(br);

            System.out.println("------  Dados da Banda  -------- \n");
            System.out.println("Nome da Banda : "+banda.getNome());
            System.out.println("Nome do Álbum : "+banda.getAlbum().getNome());
            System.out.println("Ano de Lançamento: "+banda.getAlbum().getAno());

            int count = 1;

            for (Musica musica : banda.getAlbum().getMusicas()) {
                System.out.println("Música "+ count +": "+ musica.getNome());
                count++;    
            }

            con.disconnect();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Error image:

inserir a descrição da imagem aqui

1 answer

1


The method createUnmarshaller() class javax.xml.bind.JAXBContext returns a javax.xml.bind.Unmarshaller.

But in your code you did the import of another class (which has the same name, but is in another package):

import com.sun.xml.internal.ws.util.Pool.Unmarshaller;

Just exchange for:

import javax.xml.bind.Unmarshaller;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.