gpt4 book ai didi

java - Jersey REST 日期 @XmlAttribute 导致非法参数异常

转载 作者:太空宇宙 更新时间:2023-11-04 06:59:32 26 4
gpt4 key购买 nike

我有一个 Jersey REST Web 服务,它接受 POSTed XML 字符串。我为 XML 的每个元素都有实体,以便将它们拆分为对象。当我传递日期参数时,例如InspectionDate="12/02/2012"我收到以下错误。

SEVERE: Servlet.service() for servlet [Jersey REST Service] in context with path [] threw exception
java.lang.IllegalArgumentException: 12/02/2012
at com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl$Parser.parseAndSetYear(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl$Parser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl.<init>(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.datatype.DatatypeFactoryImpl.newXMLGregorianCalendar(Unknown Source)
at com.sun.xml.bind.DatatypeConverterImpl._parseDateTime(DatatypeConverterImpl.java:400)
at com.sun.xml.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl$5.parse(RuntimeBuiltinLeafInfoImpl.java:271)
at com.sun.xml.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl$5.parse(RuntimeBuiltinLeafInfoImpl.java:273)
at com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor$CompositeTransducedAccessorImpl.parse(TransducedAccessor.java:241)
at com.sun.xml.bind.v2.runtime.unmarshaller.StructureLoader.startElement(StructureLoader.java:201)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:481)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:459)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:148)
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.XMLDocumentFragmentScannerImpl$FragmentContentDriver.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.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:211)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:124)
at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:97)
at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:393)
at com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider$EntityInjectable.getValue(EntityParamDispatchProvider.java:139)
at com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:43)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:126)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:173)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:163)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:111)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:71)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:111)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:63)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:654)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:612)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:603)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:309)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:425)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:590)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

这是我的 XML 字符串:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<user userName="Test.Test" userID="43">
<client>
<site>
<building>
<location>
<asset assetID="293">
<inspection assetID="293" inspectionDate="12/02/2012" inspectionRef="inspection_ref_89158">
<comments>Test</comments>
<assessedBy>Natasha Allsopp</assessedBy>
<assessed>false</assessed>
<notAssessedReason>Access Hatch Padlocked<notAssessedReason>
<acceptable>true</acceptable>
<remedialActionRequired>false</remedialActionRequired>
<remedialActionID>0</remedialActionID>
<flowTemperature>0.0</flowTemperature>
<returnTemperature>0.0</returnTemperature>
<temperature1MinHot>0.0</temperature1MinHot>
<temperature2MinCold>0.0</temperature2MinCold>
<showerHeadsPresent>4</showerHeadsPresent>
<showerHeadsCleaned>0</showerHeadsCleaned>
<showersNotCleanedReason></showersNotCleanedReason>
<storedWaterTemperature>0.0</storedWaterTemperature>
<incomingMainsTemperature>0.0</incomingMainsTemperature>
<anyLeaks>false</anyLeaks>
<sludgePresent>None</sludgePresent>
<corrosionPresent>None</corrosionPresent>
<flotsamPresent>None</flotsamPresent>
<turnoverCheck>Good</turnoverCheck>
<conditionofTank>Good</conditionofTank>
<goodFittingLidPresent>false</goodFittingLidPresent>
<lidVented>false</lidVented>
<ventScreened>false</ventScreened>
<overflowPresent>false</overflowPresent>
<insectScreen>false</insectScreen>
</inspection>
</asset>
</location>
</building>
</site>
</client>
</user>

这是我的实体类的片段:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"inspectionDate"})

public class Inspection {

@XmlAttribute
protected Date inspectionDate;

@XmlAttribute
protected String inspectionRef;

public Date getInspectionDate() {
return inspectionDate;
}

public void setInspectionDate(Date inspectionDate) {
this.inspectionDate = inspectionDate;
}
}

如果我使用以下 XMLJavaAdapter:

public class DateAdapter extends XmlAdapter<String, Date> {

private SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

@Override
public String marshal(Date v) throws Exception {
return dateFormat.format(v);
}

@Override
public Date unmarshal(String v) throws Exception {
return dateFormat.parse(v);
}

}

我收到以下错误:

SEVERE: Servlet.service() for servlet [Jersey REST Service] in context with path [] threw exception [com.sun.jersey.api.container.MappableContainerException: java.io.IOException: Error unmarshalling JAXB object of type "class dao.jpa.User".] with root cause
com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
There are two properties named "inspectionDate"
this problem is related to the following location:
at public java.util.Date dao.jpa.Inspection.getInspectionDate()
at dao.jpa.Inspection
at protected dao.jpa.Inspection dao.jpa.Asset.inspection
at dao.jpa.Asset
at protected dao.jpa.Asset dao.jpa.Location.asset
at dao.jpa.Location
at protected dao.jpa.Location dao.jpa.Building.location
at dao.jpa.Building
at protected dao.jpa.Building dao.jpa.Site.building
at dao.jpa.Site
at protected dao.jpa.Site dao.jpa.Client.site
at dao.jpa.Client
at protected dao.jpa.Client dao.jpa.User.client
at dao.jpa.User
this problem is related to the following location:
at protected java.util.Date dao.jpa.Inspection.inspectionDate
at dao.jpa.Inspection
at protected dao.jpa.Inspection dao.jpa.Asset.inspection
at dao.jpa.Asset
at protected dao.jpa.Asset dao.jpa.Location.asset
at dao.jpa.Location
at protected dao.jpa.Location dao.jpa.Building.location
at dao.jpa.Building
at protected dao.jpa.Building dao.jpa.Site.building
at dao.jpa.Site
at protected dao.jpa.Site dao.jpa.Client.site
at dao.jpa.Client
at protected dao.jpa.Client dao.jpa.User.client
at dao.jpa.User
Class has two properties of the same name "inspectionDate"
this problem is related to the following location:
at public java.util.Date dao.jpa.Inspection.getInspectionDate()
at dao.jpa.Inspection
at protected dao.jpa.Inspection dao.jpa.Asset.inspection
at dao.jpa.Asset
at protected dao.jpa.Asset dao.jpa.Location.asset
at dao.jpa.Location
at protected dao.jpa.Location dao.jpa.Building.location
at dao.jpa.Building
at protected dao.jpa.Building dao.jpa.Site.building
at dao.jpa.Site
at protected dao.jpa.Site dao.jpa.Client.site
at dao.jpa.Client
at protected dao.jpa.Client dao.jpa.User.client
at dao.jpa.User
this problem is related to the following location:
at protected java.util.Date dao.jpa.Inspection.inspectionDate
at dao.jpa.Inspection
at protected dao.jpa.Inspection dao.jpa.Asset.inspection
at dao.jpa.Asset
at protected dao.jpa.Asset dao.jpa.Location.asset
at dao.jpa.Location
at protected dao.jpa.Location dao.jpa.Building.location
at dao.jpa.Building
at protected dao.jpa.Building dao.jpa.Site.building
at dao.jpa.Site
at protected dao.jpa.Site dao.jpa.Client.site
at dao.jpa.Client
at protected dao.jpa.Client dao.jpa.User.client
at dao.jpa.User

at com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:102)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:472)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:302)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1140)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:154)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:121)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
at javax.xml.bind.ContextFinder.find(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getStoredJAXBContext(AbstractJAXBProvider.java:199)
at com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getJAXBContext(AbstractJAXBProvider.java:177)
at com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getUnmarshaller(AbstractJAXBProvider.java:131)
at com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getUnmarshaller(AbstractJAXBProvider.java:104)
at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:97)
at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:393)
at com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider$EntityInjectable.getValue(EntityParamDispatchProvider.java:139)
at com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:43)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:126)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:173)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:163)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:111)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:71)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:111)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:63)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:654)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:612)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:603)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:309)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:425)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:590)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

如果有人有任何想法,请告诉我。

最佳答案

我的猜测是,您正在获取两个属性...,异常(exception)的是,在您的真实类中,您在 get 方法上添加了 @XmlJavaTypeAdapter(DateAdapter.class) 注释,而不是在具有 @XmlAttribute 注释的字段。

关于java - Jersey REST 日期 @XmlAttribute 导致非法参数异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22297928/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com