Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MismatchedInputException when parse xml to pojo #231

Closed
reusu opened this issue Apr 28, 2017 · 3 comments
Closed

MismatchedInputException when parse xml to pojo #231

reusu opened this issue Apr 28, 2017 · 3 comments
Labels
duplicate Issue is duplicate of another issue

Comments

@reusu
Copy link

reusu commented Apr 28, 2017

jackson-dataformat-2.9.0.pr2.jar

1.readValue:
<?xml version="1.0" encoding="ISO-8859-1"?> <profile> <registrations> </registrations> </profile>
https://www.v2ex.com/i/wBH8y6GW.jpeg

return:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not construct instance of entity.api.statusXstream.StatusXMLRegistrations (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('
')
at [Source: (StringReader); line: 4, column: 3] (through reference chain: com.shenou.fs.core.entity.api.statusXstream.StatusXMLProfile["registrations"])
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:62) ~[jackson-databind-2.9.0.pr2.jar:2.9.0.pr2]
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1307) ~[jackson-databind-2.9.0.pr2.jar:2.9.0.pr2]
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1009) ~[jackson-databind-2.9.0.pr2.jar:2.9.0.pr2]
at com.fasterxml.jackson.databind.deser.ValueInstantiator._createFromStringFallbacks(ValueInstantiator.java:370) ~[jackson-databind-2.9.0.pr2.jar:2.9.0.pr2]
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:316) ~[jackson-databind-2.9.0.pr2.jar:2.9.0.pr2]
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1359) ~[jackson-databind-2.9.0.pr2.jar:2.9.0.pr2]
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:158) ~[jackson-databind-2.9.0.pr2.jar:2.9.0.pr2]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:149) ~[jackson-databind-2.9.0.pr2.jar:2.9.0.pr2]
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:124) ~[jackson-databind-2.9.0.pr2.jar:2.9.0.pr2]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:275) ~[jackson-databind-2.9.0.pr2.jar:2.9.0.pr2]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:139) ~[jackson-databind-2.9.0.pr2.jar:2.9.0.pr2]
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3944) ~[jackson-databind-2.9.0.pr2.jar:2.9.0.pr2]
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2925) ~[jackson-databind-2.9.0.pr2.jar:2.9.0.pr2]

2.readValue:
https://www.v2ex.com/i/IuXfN5KB.jpeg
<?xml version="1.0" encoding="ISO-8859-1"?><profile><registrations></registrations></profile>

return:
secuucess


but i usually parse type 1 xml
how to fix it

@cowtowncoder
Copy link
Member

Please include code to reproduce the problem.

@reusu
Copy link
Author

reusu commented May 2, 2017

XmlUtils.java

public class XmlUtils {
	private static Logger logger = LogManager.getLogger(XmlUtils.class);
	private static XmlMapper xmlMapper = new XmlMapper();
	static{
		xmlMapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, true);
		xmlMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
		xmlMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
		xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		xmlMapper.setSerializationInclusion(Include.NON_NULL);
	}
	private XmlUtils(){}
	public static Object getVo(String xml, Class<?> inClass){
		try{
			return xmlMapper.readValue(xml, inClass);
		}catch (Exception e) {
			logger.error("JacksonXML Error",e);
			return null;
		}
	}
	public static String toReturnXml(Object obj){
		try{
			return xmlMapper.writeValueAsString(obj);
		}catch (Exception e) {
			logger.error("JacksonXML Error",e);
			return "";
		}
	}
}

StatusXMLProfile..java

@JacksonXmlRootElement(localName = "profile")
public class StatusXMLProfile implements Serializable{
	private static final long serialVersionUID = 1L;
	private StatusXMLRegistrations registrations;
	public StatusXMLRegistrations getRegistrations() {
		return registrations;
	}
	public void setRegistrations(StatusXMLRegistrations registrations) {
		this.registrations = registrations;
	}
}

test.java

	public static void main(String[] args) throws Exception {
		//type1 throw exception
		String xml = "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iSVNPLTg4NTktMSI/Pg0KPHByb2ZpbGU+DQogIDxyZWdpc3RyYXRpb25zPg0KICA8L3JlZ2lzdHJhdGlvbnM+DQo8L3Byb2ZpbGU+";
		xml = new String(Base64.getDecoder().decode(xml),"UTF-8");
		System.out.println(xml);
		StatusXMLProfile sxv = (StatusXMLProfile)XmlUtils.getVo(xml, StatusXMLProfile.class);
		//type2 secucess
		xml = "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iSVNPLTg4NTktMSI/Pjxwcm9maWxlPjxyZWdpc3RyYXRpb25zPjwvcmVnaXN0cmF0aW9ucz48L3Byb2ZpbGU+";
		xml = new String(Base64.getDecoder().decode(xml),"UTF-8");
		System.out.println(xml);
		sxv = (StatusXMLProfile)XmlUtils.getVo(xml, StatusXMLProfile.class);
	}

@cowtowncoder
Copy link
Member

Actually this is the same issue as #318: closing this one since the other issue has matching tests; this would get resolved at same time.

@cowtowncoder cowtowncoder added the duplicate Issue is duplicate of another issue label Apr 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Issue is duplicate of another issue
Projects
None yet
Development

No branches or pull requests

2 participants