2013-02-22 94 views
1

將JSON數據映射到POJO類時出現問題。客戶類有兩個字段「customerId,isTrue)。我爲每個字段使用JacksonMixInAnnotations。Jackson Object Mapper和JSON

沒有JSON的第一部分({\」customer \「:」),JUnit測試可以。如果包含,則失敗(對於customer.getCustomerId()爲null)。

感謝

@Test 
public void test() { 
    mockServer.expect(requestTo("...")) 
     .andExpect(method(GET)) 
     .andRespond(withSuccess(str, MediaType.APPLICATION_JSON)); 
    Customer customer = api.operations().getCustomer(); 
    assertEquals("foo", customer.getCustomerId()); 
    assertEquals("TRUE"),customer.isTrue()); 
} 

String str = "{ \"customer\": { \"customer-id\": \"foo\",\"is-true\": \"TRUE\" }}"; 

@Override 
protected MappingJacksonHttpMessageConverter getJsonMessageConverter() { 
    MappingJacksonHttpMessageConverter converter = super.getJsonMessageConverter(); 
    objectMapper = new ObjectMapper(); 
    objectMapper.registerModule(new TestModule()); 
    converter.setObjectMapper(objectMapper); 
    return converter; 
} 

如果我添加一個「客戶」字段POJO然後我得到以下錯誤:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of java.lang.String out of START_OBJECT token 
at [Source: [email protected]; line: 1, column: 3]; nested exception is org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token 
at [Source: [email protected]; line: 1, column: 3] 
    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readJavaType(MappingJacksonHttpMessageConverter.java:169) 
    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.read(MappingJacksonHttpMessageConverter.java:161) 
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:94) 
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:491) 
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:460) 
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:228) 
    at org.springframework.social.test.api.impl.CustomerTemplate.getCustomer(CustomerTemplate.java:24) 
    at org.springframework.social.test.api.impl.CustomerTemplateTest.getCustomer_current_user(CustomerTemplateTest.java:20) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 
    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.map.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token 
at [Source: [email protected]; line: 1, column: 3] 
    at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163) 
    at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219) 
    at org.codehaus.jackson.map.deser.std.StringDeserializer.deserialize(StringDeserializer.java:44) 
    at org.codehaus.jackson.map.deser.std.StringDeserializer.deserialize(StringDeserializer.java:13) 
    at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299) 
    at org.codehaus.jackson.map.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:905) 
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:739) 
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:683) 
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580) 
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732) 
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1923) 
    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readJavaType(MappingJacksonHttpMessageConverter.java:166) 
    ... 31 more 

回答

2

看一看在@JsonRootName annotation(將其添加到您的Customer.class )並使用方法enable(DeserializationFeature feature)ObjectMapper上啓用DeserializationFeatureUNWRAP_ROOT_VALUE

+0

你能告訴我解釋最新消息嗎?謝謝 – user1377822 2013-02-22 14:04:39

+0

objectMapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); < - 給我...類型ObjectMapper中的方法enable(DeserializationConfig.Feature ...)不適用於參數(DeserializationFeature) – user1377822 2013-02-22 14:32:06

+0

不要混合不同版本的Jackson。如果您使用的是版本1.x,則可以使用[DeserializationConfig.Feature.UNWRAP_ROOT_VALUE](http://jackson.codehaus.org/1.9.4/javadoc/org/codehaus/jackson/map/DeserializationConfig.Feature.html#UNWRAP_ROOT_VALUE )。 – nutlike 2013-02-22 14:46:35