Spring 3.1中的UnmarshalException使用xml请求的Restful服务

时间:2012-12-03 05:09:49

标签: spring jaxb2 spring-oxm

我使用spring web 3.1.1和spring oxm 3.1.1。

当jaxb2marshaller尝试将xml源解组为object时 发生例外情况。

2012-12-03 13:38:41,152[k.c.s.s.c.r.AuthenticationController:154][ERROR] JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException: Namespace URIs and local names to the unmarshaller needs to be interned
org.springframework.oxm.UnmarshallingFailureException: JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException: Namespace URIs and local names to the unmarshaller needs to be interned
at org.springframework.oxm.jaxb.Jaxb2Marshaller.convertJaxbException(Jaxb2Marshaller.java:761) ~[org.springframework.oxm-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:682) ~[org.springframework.oxm-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:665) ~[org.springframework.oxm-3.1.1.RELEASE.jar:3.1.1.RELEASE]

我在我的sevlet设置上配置了jaxb2 marshall。

<oxm:jaxb2-marshaller id="marshaller">
    <oxm:class-to-be-bound name="kr.co.skcomms.simon.bean.rest.Authentication" />
</oxm:jaxb2-marshaller>

并且在Controller上尝试解组这样的。

@Autowired
public Jaxb2Marshaller marshaller;
....
Source source = new StreamSource(new StringReader(body));
Authentication authentication = (Authentication) marshaller.unmarshal(source);

和我的身份验证对象。

@XmlRootElement
public class Authentication {
private String simon_auth;

private String client_ip;

private String request_url;

public Authentication(){
}

@XmlElement
public String getSimon_auth() {
    return simon_auth;
}

public void setSimon_auth(String simon_auth) {
    this.simon_auth = simon_auth;
}

@XmlElement
public String getClient_ip() {
    return client_ip;
}

public void setClient_ip(String client_ip) {
    this.client_ip = client_ip;
}

@XmlElement
public String getRequest_url() {
    return request_url;
}

public void setRequest_url(String request_url) {
    this.request_url = request_url;
}

@XmlRootElement public class Authentication { private String simon_auth; private String client_ip; private String request_url; public Authentication(){ } @XmlElement public String getSimon_auth() { return simon_auth; } public void setSimon_auth(String simon_auth) { this.simon_auth = simon_auth; } @XmlElement public String getClient_ip() { return client_ip; } public void setClient_ip(String client_ip) { this.client_ip = client_ip; } @XmlElement public String getRequest_url() { return request_url; } public void setRequest_url(String request_url) { this.request_url = request_url; }

我提到这篇文章.. http://www.ibm.com/developerworks/webservices/library/wa-spring3webserv/index.html

我认为我的来源与文章的示例代码绝对相同。

但效果不佳。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

自己解决问题..(笑)

下载springframework-oxm-3.1.1.source的来源.. 并记录org.springframework.oxm.jaxb.Jaxb2Marshaller类的orception异常堆栈跟踪。

2012-12-03 21:28:14,864[o.s.o.j.Jaxb2Marshaller:698][DEBUG] [JAXBException] : javax.xml.bind.UnmarshalException: Namespace URIs and local names to the unmarshaller needs to be interned.
javax.xml.bind.UnmarshalException: Namespace URIs and local names to the unmarshaller needs to be interned.
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:631) ~[na:1.6.0_16]
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:236) ~[na:1.6.0_16]
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:231) ~[na:1.6.0_16]
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:103) ~[na:1.6.0_16]
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1038) ~[na:1.6.0_16]
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:467) ~[na:1.6.0_16]
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:448) ~[na:1.6.0_16]
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:137) ~[na:1.6.0_16]
    at com.caucho.xml.XmlParser.addElement(XmlParser.java:2615) ~[resin.jar:3.1.0]
    at com.caucho.xml.XmlParser.parseElement(XmlParser.java:663) ~[resin.jar:3.1.0]
    at com.caucho.xml.XmlParser.parseNode(XmlParser.java:381) ~[resin.jar:3.1.0]
    at com.caucho.xml.XmlParser.parseInt(XmlParser.java:256) ~[resin.jar:3.1.0]
    at com.caucho.xml.AbstractParser.parse(AbstractParser.java:644) ~[resin.jar:3.1.0]
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:200) ~[na:1.6.0_16]
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:173) ~[na:1.6.0_16]
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137) ~[na:1.6.0_16]
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:105) ~[na:1.6.0_16]
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:694) [classes:na]
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:672) [classes:na]
你可以看到。 java UnmarshallerImpl对象选择Resin的XmlPaser ... 这就是问题.. Resin设置了对xml处理很重要的系统属性

javax.xml.parsers.DocumentBuilderFactory
javax.xml.parsers.SAXParserFactory

这些工厂豆适用于springframework-oxm解析器。然后......它无法运作。

所以我改变了resin.conf文件(也许是[resin_dir] /conf/resin.conf)

<!-- Uncomment to use Resin's XML implementations
 -
 - <system-property javax.xml.parsers.DocumentBuilderFactory
 -                 ="com.caucho.xml.parsers.XmlDocumentBuilderFactory"/>
 - <system-property javax.xml.parsers.SAXParserFactory
 -                 ="com.caucho.xml.parsers.XmlSAXParserFactory"/>
-->

结论   Resin的XmlPaser和其他工厂没有springframework-oxm库的灵活性。

我希望你不要浪费你的时间!