尝试将XML有效内容分配给java对象时,我收到了ConversionException。我想要做的就是将一个名单列表添加到属于DTO的List对象中。有人可以帮我解决这个问题吗?我看了一些其他的帖子,但还没有得到解决方案。这是我的代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try (PrintWriter pw = response.getWriter())
{
InputDTO inputDTO = Reader.inputFromXML(request);
pw.println("test");
}
}
读者类
public class Reader {
private static final XStream xstream = new XStream(new DomDriver("UTF-8")) {
{
//this.processAnnotations(InputDTO.class);
}
};
public static InputDTO inputFromXML(HttpServletRequest request) throws IOException {
InputDTO input = null;
try (InputStream is = request.getInputStream())
{
xstream.alias("request", InputDTO.class);
input = (InputDTO) xstream.fromXML(is);
}
return input;
}
}
InputDTO:
public class InputDTO {
@XStreamImplicit(itemFieldName = "names")
private List<String> names;
public List<String> getNames() {
return names;
}
public void setNames(List<String> names) {
this.names = names;
}
}
这是我发送的请求:
<request>
<names>
<name>Craig</name>
<name>John</name>
</names>
</request>
这是完整的堆栈跟踪。
SEVERE: Servlet.service() for servlet ListOfNames threw exception
com.thoughtworks.xstream.converters.ConversionException: name : name : name : name
---- Debugging information ----
message : name : name
cause-exception : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message : name : name
class : com.hd.testing.InputDTO
required-type : java.util.ArrayList
path : /request/names/name
-------------------------------
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:89)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:63)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:246)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:218)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:162)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:63)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:60)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:137)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:33)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:923)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:909)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:861)
at com.hd.testing.Reader.inputFromXML(Reader.java:26)
at com.hd.testing.ListOfNames.doPost(ListOfNames.java:40)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at com.hd.ta.aa.dao.catalina.THDDAOValve.invoke(Unknown Source)
at com.hd.ta.aa.catalina.realm.THDSingleSignOn.invoke(THDSingleSignOn.java:631)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:724)
Caused by: com.thoughtworks.xstream.mapper.CannotResolveClassException: name : name
at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:68)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:71)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:86)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:96)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)
at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:52)
at com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29)
at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:70)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:68)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.unmarshal(CollectionConverter.java:61)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82)
... 32 more
答案 0 :(得分:4)
起初,这与Servlets无关。
您的@XStreamImplicit(itemFieldName = "names")
代码将打印出来(如果您将InputDTO
的别名设为"request"
)
<request>
<names>Craig</names>
<names>John</names>
</request>
但是如果你想将名字包装成一个元素,你可以使用NamedCollectionsConverter
:
final NamedCollectionConverter namesConverter = new NamedCollectionConverter(
xstream.getMapper(), "name", String.class);
xstream.registerLocalConverter(InputDTO.class, "names", namesConverter);
以下是完整的工作示例:
public class DTOTest {
public static void main(String[] args) {
final XStream xstream = new XStream();
xstream.processAnnotations(InputDTO.class);
final NamedCollectionConverter namesConverter = new NamedCollectionConverter(
xstream.getMapper(), "name", String.class);
xstream.registerLocalConverter(InputDTO.class, "names", namesConverter);
final InputDTO inputDTO = new InputDTO();
final List<String> names = new ArrayList<String>();
names.add("Craig");
names.add("John");
// inputDTO.setNames(Arrays.asList("Craig", "John")); doesn't work!
inputDTO.setNames(names);
final String xml = xstream.toXML(inputDTO);
System.out.println(xml);
System.out.println();
final InputDTO inputDTO2 = (InputDTO) xstream.fromXML(xml);
for (String name : inputDTO2.getNames()) {
System.out.println(name);
}
}
}
@XStreamAlias("request")
class InputDTO {
private List<String> names;
public List<String> getNames() {
return names;
}
public void setNames(List<String> names) {
this.names = names;
}
}
打印出来:
<request>
<names>
<name>Craig</name>
<name>John</name>
</names>
</request>
Craig
John
我不知道如何使用注释而不是registerConverter(...)
来设置此转换器
@XStreamConverter(value = NamedCollectionConverter.class, types = { String.class }, strings = { "name" })
private List<String> names;
无法转换ArrayLists
。