Invoking Web-Service without using JAX-RS
I have a Web-Service with Restfull Jax-rs and I have created WSClient with Restfull but without using Jax-rs. I am using Spring3.0, Tomcat. Here is my WSClient:
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;
@Controller
public class WSClientController {
@Inject
@Named("restTemplate")
protected RestTemplate restTemplate;
private final static String articleServiceUrl = "http://HOST:8080/WS/Service/getAccounts.html";
@RequestMapping(value={"/accountMasterRoot"}, method={org.springframework.web.bind.annotation.RequestMethod.GET}, headers={"Accept=application/xml, application/json"})
public List<AccountMasterWS> getAccountMasterRoot() {
try {
AccountMasterRoot ac = restTemplate.getForObject(articleServiceUrl, AccountMasterRoot.class);
List<AccountMasterWS> listAccounts = ac.getAccountMasterWS();
return listAccounts;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
but when I try to invoke Web-Service with this code I am getting error like :
org.springframework.http.converter.HttpMessageNotReadableException: Could not read [class com.nmmc.fas.ws.model.AccountMasterRoot]; nested exception is org.springframework.oxm.UnmarshallingFailureException: XStream unmarshalling exception; nested exception is com.thoughtworks.xstream.mapper.CannotResolveClassException: accountMasterRoot : accountMasterRoot
at org.springframework.http.converter.xml.MarshallingHttpMessageConverter.readFromSource(MarshallingHttpMessageConverter.java:116)
at org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter.readInternal(AbstractXmlHttpMessageConverter.java:61)
at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:152)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:60)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:352)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:307)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:177)
at com.nmmc.ws.controller.WSClientController.getAccountMasterRoot(WSClientController.java:42)
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:616)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
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:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:636)
Caused by: org.springframework.oxm.UnmarshallingFailureException: XStream unmarshalling exception; nested exception is com.thoughtworks.xstream.mapper.CannotResolveClassException: accountMasterRoot : accountMasterRoot
at org.springframework.oxm.xstream.XStreamMarshaller.convertXStreamException(XStreamMarshaller.java:498)
at org.springframework.oxm.xstream.XStreamMarshaller.unmarshal(XStreamMarshaller.java:476)
at org.springframework.oxm.xstream.XStreamMarshaller.unmarshalReader(XStreamMarshaller.java:459)
at org.springframework.oxm.xstream.XStreamMarshaller.unmarshalInputStream(XStreamMarshaller.java:450)
at org.springframework.oxm.support.AbstractMarshaller.unmarshalStreamSourc开发者_如何学JAVAe(AbstractMarshaller.java:368)
at org.springframework.oxm.support.AbstractMarshaller.unmarshal(AbstractMarshaller.java:134)
at org.springframework.http.converter.xml.MarshallingHttpMessageConverter.readFromSource(MarshallingHttpMessageConverter.java:113)
... 37 more
Caused by: com.thoughtworks.xstream.mapper.CannotResolveClassException: accountMasterRoot : accountMasterRoot
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.core.TreeUnmarshaller.start(TreeUnmarshaller.java:136)
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:899)
at org.springframework.oxm.xstream.XStreamMarshaller.unmarshal(XStreamMarshaller.java:473)
I am totally stuck here, please help me what should I do changes in my code...
Hi here I am able to find out the solution now its working when I used webClient. My controller snipped like:
@RequestMapping(value = { "/accountMasterRoot.etenderingWS" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
public List<AccountMasterWS> getAccountMasterRoot() {
try {
WebClient client = WebClient.create(articleServiceUrl);
AccountMasterRoot a = client.accept("application/xml").get(
AccountMasterRoot.class);
List<AccountMasterWS> wsl = a.getAccountMasterWS();
for (int i = 0; i < wsl.size(); i++) {
Long code = wsl.get(i).getCode();
String name = wsl.get(i).getName();
System.out.println("Code " + code + " name " + name);
}
System.out.println("End of WebService");
return wsl;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
<import resource="etendering-servlet.xml" />
<context:component-scan base-package="com.nmmc.ws" />
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<constructor-arg>
<bean class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="aliases"> <props> <prop key="listAccounts">com.nmmc.fas.ws.model.AccountMasterRoot</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.nmmc.fas.ws.model.AccountMasterRoot</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
</list>
</property>
</bean>
精彩评论