Bonjour, lorsque j'exécute mon code de test, j'ai une exception qui est lancée lors de l'étape de unmarshal par JAXB.
Je n'arrive pas à trouver la solution du problème et Google n'est pas très bavard à ce sujet, c'est pourquoi je requiert votre expertise.
Voici le code d'erreur :
Voici comment je fais le test :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 Exception in thread "main" javax.xml.bind.UnmarshalException: Unable to create an instance of main.**.Project - with linked exception: [java.lang.InstantiationException] at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StructureLoader.startElement(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source) at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source) at main.java.***.Client.unmarshalNonGeneric(Client.java:401) at main.java.***.Client.doSomething(Client.java:349) at main.***.Client.main(Client.java:66) Caused by: java.lang.InstantiationException at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48) at java.lang.reflect.Constructor.newInstance(Constructor.java:532) at com.sun.xml.internal.bind.v2.ClassFactory.create0(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.createInstance(Unknown Source) ... 25 more
Et voici la classe Project en question :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 import java.io.IOException; import java.io.InputStream; import java.net.ConnectException; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import main.java.**.Project; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.cxf.common.util.Base64Utility; public class Client { // private Client() { // } public static void main(String[] args) throws Exception { String url = "http://localhost:8080/projects/127"; String login = "toto"; String password = "tata"; doSomething(url, login, password); } public static void doSomething(String url, String login, String password ) throws InterruptedException, HttpException, IOException, JAXBException{ GetMethod get = new GetMethod(url); get.addRequestHeader("Accept", "application/xml"); get.addRequestHeader("ContentType", "application/xml"); // Je construit le header HTTP Authorization get.addRequestHeader("Authorization", getHeaderAuthorization(login, password)); HttpClient httpclient = new HttpClient(); try { int result = httpclient.executeMethod(get); System.out.println("\nResponse status code:\n" + result); InputStream stream = get.getResponseBodyAsStream(); Project project = (Project)unmarshal(Project.class, stream); System.out.println("Project is:" + project.toString()); } catch (ConnectException e) { System.out.println("\nConnection to service refused !\n" + "Please, check that server is running and URL and port are right."); } finally { get.releaseConnection(); } } public String getHeaderAuthorization(String login, String password) { String loginPassword = login + ":" + password; String encodedLoginPassword = Base64Utility.encode(loginPassword.getBytes()); String encodedHeader = "Basic " + encodedLoginPassword; return encodedHeader; } public static <T> T unmarshal(Class<T> docClass, InputStream inputStream) throws JAXBException { System.out.println("Class is: " + docClass); JAXBContext jc = JAXBContext.newInstance(docClass); Unmarshaller u = jc.createUnmarshaller(); T object = (T) u.unmarshal(inputStream); return (T) object; }
Le XML qui est renvoyé par le serveur est correct, c'est par exemple :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 import javax.xml.bind.annotation.XmlRootElement; import main.***.ProticObject; @XmlRootElement(name = "project") public abstract class Project extends ProticObject { protected String projectName; protected String projectDescription; protected String remark; protected boolean isPrivate; public Project() { } public int getProjectId() { return projectId; } public void setProjectId(int projectId) { this.projectId = projectId; } public String getProjectName() { return projectName; } public void setProjectName(String projectName) { this.projectName = projectName; } public String getProjectDescription() { return projectDescription; } public void setProjectDescription(String projectDescription) { this.projectDescription = projectDescription; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public boolean isPrivate() { return isPrivate; } public void setPrivate(boolean isPrivate) { this.isPrivate = isPrivate; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 <project> <dbId>127</dbId> <logId>0</logId> <projectId>127</projectId> <private>false</private> <projectDescription> Here is the description of the project... </projectDescription> <projectName>ARABIDOPSIS</projectName> <remark> Abstract: Here is the abstract... </remark> </project>
Quelqu'un aurait-il une idée de comment régler le problème ?
Merci de votre aide.
Partager