JAXB同代码跨项目反序列化异常问题求助
Hey Rodrigo, let's dig into why this unexpected namespace issue is popping up when you moved your JAXB code to a new project, and walk through actionable fixes to get things working again.
What's Causing the Problem?
The key clue here is that your Form class is inheriting a namespace from somewhere else in the new project—even though you didn't explicitly set it on the class itself. The most likely culprit is:
- Parent package namespace inheritance: Your
pt.interfaces.formpackage is a subpackage ofpt.interfaces, which has its ownpackage-info.javawith thehttp://www.site1.pt/namespace. JAXB automatically inherits parent package's XML schema configurations if the subpackage doesn't have its ownpackage-info.javato override it. That's exactly why your unmarshaller started expecting the<{http://www.site1.pt/}Form>element instead of the empty-namespace one from your XML.
Step-by-Step Fixes
1. Override Namespace in the Subpackage's package-info.java
Create or update pt.interfaces.form/package-info.java to explicitly set an empty namespace and unqualified element form (so JAXB matches elements without namespace prefixes):
@XmlSchema( namespace = "", elementFormDefault = XmlNsForm.UNQUALIFIED ) package pt.interfaces.form; import javax.xml.bind.annotation.XmlNsForm; import javax.xml.bind.annotation.XmlSchema;
This tells JAXB that all classes in this subpackage should use an empty namespace, completely overriding the parent package's configuration.
2. Explicitly Set Namespace on the Form Class
If you don't want to deal with package-level config, you can directly annotate the Form class to force an empty namespace:
import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(namespace = "") public class Form { // Your existing class code here }
This bypasses any inherited namespace settings and ensures the Form root element matches the empty-namespace element in your XML.
3. Fix a Critical Code Mistake in Unmarshalling
Wait, I noticed a small but important error in your code: you're using StringWriter to unmarshal from a string, but StringWriter is for writing XML output, not reading it. Swap it with StringReader—this might also be why your Form object wasn't getting values even when there was no error:
JAXBContext jaxbContext = JAXBContext.newInstance(Form.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); // Use StringReader instead of StringWriter for unmarshalling Form form = (Form) jaxbUnmarshaller.unmarshal(new StringReader(xmlstring));
4. Verify Your XML Structure
Double-check that your input XML's <Form> element has no namespace declarations or prefixes. It should look like this:
<Form> <!-- Your form fields here --> </Form>
Based on your error message, your XML already has an empty URI, so this shouldn't be an issue—but it's worth confirming to rule out hidden namespace declarations.
Why Your Previous Attempts Didn't Work
- When you set the subpackage's namespace to
http://www.test.pt/, JAXB just switched to expecting that new namespace instead of the empty one, hence the updated error. - When you removed the
package-info.javaor set an empty namespace withoutelementFormDefault = XmlNsForm.UNQUALIFIED, JAXB might still have been expecting "qualified" elements (with namespace prefixes), leading to failed value mapping even without explicit errors.
内容的提问来源于stack exchange,提问作者Rodrigo Rapozo

