我的网络应用程序正在使用Apache CXF 和 JAVA8,如果用户发送,则会面临以下响应错误xs:datetime
输入(秒 00)为
<urn1:dateTimeVal>2016-04-29T20:00:00</urn1:dateTimeVal>
ERROR :
org.apache.cxf.interceptor.Fault:编组错误:
cvc-datatype-valid.1.2.1:“2016-04-29T20:00”不是有效值
'约会时间'。
我调试并分析了如果用户发送dateTimeVal
as 2016-04-29T20:00:00
然后输入的 CXF 验证通过并且 XML 值是解组 to java.time.LocalDateTime
as 2016-05-05T20:00
,并且在返回响应时,编组由于丢失秒部分(00)而发生错误。
任何帮助/提示表示赞赏。
P.S:您可以尝试使用以下代码片段:
java.time.LocalDateTime dt= java.time.LocalDateTime.of(2016, Month.MAY, 5, 20, 00, 00);
System.out.println(dt);
Note:上面的代码示例仅用于理解打印日期时间值。但 Web 应用程序中预期的实际返回类型是java.time.LocalDateTime
预期产出: 2016-05-05T20:00:00
实际输出: 2016-05-05T20:00
EDIT:该字段的绑定 (JAXB) 内容是:
@XmlElement(required = true, type = String.class)
@XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
@XmlSchemaType(name = "dateTime")
@Generated(value = "com.sun.tools.xjc.Driver", date = "2016-05-03T05:28:57+05:30", comments = "JAXB RI v2.2.11")
@NotNull
protected LocalDateTime dateTimeVal;
AND LocalDateTimeAdapter
文件是
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class LocalDateTimeAdapter
extends XmlAdapter<String, LocalDateTime>
{
public static LocalDateTime parse(String value)
{
DateTimeFormatter dateTimeAndZoneformatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
DateTimeFormatter dateTimeformatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
TemporalAccessor ta = null;
try
{
ta = dateTimeformatter.parse(value);
}
catch (DateTimeParseException ex)
{
ta = dateTimeAndZoneformatter.parse(value);
}
return LocalDateTime.from(ta);
}
public static String print(LocalDateTime value)
{
return value.toString();
}
public LocalDateTime unmarshal(String value)
{
return parse(value);
}
public String marshal(LocalDateTime value)
{
return print(value);
}
}