2014-10-17 103 views
0

我有幾個PJO代表Apache CXF Web服務(2.7.2)中的響應對象。我所有的根元素類都註釋了@XmlRootElementCXF/RestTemplate反序列化問題

這裏是POJO類:

@XmlRootElement(name="SearchResponse") 
public class SearchResponse extends Response { 
    private Listing[] listings; 
    private SearchSummary summary; 
... 
} 

@XmlRootElement(name="SearchResponseRestricted") 
public class SearchResponseRestricted extends Response { 

    private ListingRestricted[] listings; 
    private SearchSummary summary; 
... 
} 

使用@Autowired public RestTemplate getRESTClient();,我可以成功地調用返回SearchResponseRestricted結果的方法,但不是SearchResponse

SearchResponseRestricted response = getRESTClient().postForObject(getUrlForJSON()+"/findRestricted", generateJSONRequest(request), SearchResponseRestricted.class); <-- Success 

以上成功,而下面沒有反序列化:

SearchResponse response = getRESTClient().postForObject(getUrlForJSON()+"/find", generateJSONRequest(request), SearchResponse.class); <-- Fails 

generateJSONRequest爲簡潔:

public HttpEntity<Object> generateJSONRequest(Object object) { 
    HttpHeaders headers = new HttpHeaders(); 
    headers.setContentType(MediaType.APPLICATION_JSON); 
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); 
    return new HttpEntity<Object>(object, headers); 
} 

值得一提的,無論是方法調用從Web服務返回JSON回並且JSON數據看起來應該代表應該被反序列化的類

當失敗反序列化方法被調用時,我得到以下異常:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.pkg.ws.pub.SearchResponse] and content type [application/json] at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:108) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:535) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:489) at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:318) at com.pkg.ws.service.SearchServiceWSTest.testFind(SearchServiceWSTest.java:362) 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:606) at junit.framework.TestCase.runTest(TestCase.java:176) at junit.framework.TestCase.runBare(TestCase.java:141) at junit.framework.TestResult$1.protect(TestResult.java:122) at junit.framework.TestResult.runProtected(TestResult.java:142) at junit.framework.TestResult.run(TestResult.java:125) at junit.framework.TestCase.run(TestCase.java:129) at junit.framework.TestSuite.runTest(TestSuite.java:255) at junit.framework.TestSuite.run(TestSuite.java:250) at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84) 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)

任何人都可以點我在正確的方向或暗示什麼,我可俯瞰?

回答

0

所以我找出了錯在哪裏。 在沒有課,Listing對象有一個領域是它的子類的抽象類(Region

我最後乾脆不必增加了一些額外的註解列出了已知子類:

@XmlSeeAlso({RegionAlt.class,RegionGeneric.class,RegionLBA.class,RegionGeom.class,RegionLoc.class,RegionMulti.class,RegionRad.class,RegionStyle.class,RegionPC.class,RegionHi.class,RegionIntersec.class}) 
@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public abstract class Region extends .... implements... { 
    ... 
}