Jackson Unexpected character ('h' (code 104)): actual value expected

I am accessing the REST API using a URL that gives me the following JSON result:

{"size":1,"filter":{"applicationName":"xx.x1", "fromTimestamp":1261746800000, "toTimestamp":1361833200000, "company":"xx", "groupedBy":"COMPANY_APPLICATION"}, "values"[{"applicationName":"xx.x1","count":17,"company":"xx"}], "start":0,"limit":25,"lastPage":true} 

Using Jackson, I am trying to parse json this way:

  ObjectMapper mapper = new ObjectMapper(); try { ErrorDataRest error = mapper.readValue(url, ErrorDataRest.class); Map<String,Object> map = mapper.readValue(url, Map.class); 

It doesn't matter if I use the first or second, I get this exception:

 dk.jyskebank.jee.core.exception.JyskeSystemException: Auto generated exception at dk.sd.dumpmonitor.domainservice.RestErrorServiceBean.queryRESTurl(RestErrorServiceBean.java:46) at service.RestErrorServiceTest.testQueryRESTurl(RestErrorServiceTest.java:19) 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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: org.codehaus.jackson.JsonParseException: Unexpected character ('h' (code 104)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [Source: java.io.StringReader@30d82d ; line: 1, column: 2] at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1433) at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:521) at org.codehaus.jackson.impl.JsonParserMinimalBase._reportUnexpectedChar(JsonParserMinimalBase.java:442) at org.codehaus.jackson.impl.ReaderBasedParser._handleUnexpectedValue(ReaderBasedParser.java:1198) at org.codehaus.jackson.impl.ReaderBasedParser.nextToken(ReaderBasedParser.java:485) at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2770) at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2718) at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1863) at dk.sd.dumpmonitor.domainservice.RestErrorServiceBean.queryRESTurl(RestErrorServiceBean.java:43) ... 25 more 

I do not know what is wrong or how to fix it.

I do not have access to the remainder API, but I know that it uses RESTeasy.

The only thing I want is to map the JSON response to the Java class, maybe there is another way to do this?

+7
source share
2 answers

I assume your url variable is a String , not a url , so Jackson parses the URL as if it were a real JSON string. This explains the "Unexpected character" h ", as this is apparently the first letter of your URL (i.e. Http ... something.) The code" 104 "matches the ASCII value of" h ".

If you use mapper.readValue(new URL(url), ErrorDataRest.class); Does this work?

+24
source

This happened to me after I (clumsy and stupid) used echo to get the XML into a test file:

 echo "<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"><s:Envelope>" \ > sampleXml 

Of course, he lost all the quotes and caused this problem for me.

Make sure all your attributes have their quotes!

0
source

All Articles