Exception while exposing a bean in webservice using spring mvc

Posted by Ajay on Stack Overflow See other posts from Stack Overflow or by Ajay
Published on 2011-01-15T10:41:51Z Indexed on 2011/01/15 10:53 UTC
Read the original article Hit count: 183

Hi,

I am using Spring 3.0.5.Release MVC for exposing a webservice and below is my servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- To enable @RequestMapping process on type level and method level -->
<context:component-scan base-package="com.pyramid.qls.progressReporter.service" />

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  <property name="messageConverters">
    <list>
      <ref bean="marshallingConverter" />
      <ref bean="atomConverter"  />
      <ref bean="jsonConverter" />
    </list>
  </property>
</bean>

<bean id="marshallingConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
  <constructor-arg ref="jaxbMarshaller" />
  <property name="supportedMediaTypes" value="application/xml"/>
</bean>

<bean id="atomConverter" class="org.springframework.http.converter.feed.AtomFeedHttpMessageConverter">
  <property name="supportedMediaTypes" value="application/atom+xml" />
</bean>

<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
  <property name="supportedMediaTypes" value="application/json" />
</bean>

<!-- Client -->
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
  <property name="messageConverters">
    <list>
      <ref bean="marshallingConverter" />
      <ref bean="atomConverter"  />
      <ref bean="jsonConverter" />
    </list>
  </property>
</bean>

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
  <property name="classesToBeBound">
    <list>
      <value>com.pyramid.qls.progressReporter.impl.BatchProgressMetricsImpl</value>
      <value>com.pyramid.qls.progressReporter.datatype.InstrumentStats</value>
      <value>com.pyramid.qls.progressReporter.datatype.InstrumentInfo</value>
      <value>com.pyramid.qls.progressReporter.datatype.LoadOnConsumer</value>
      <value>com.pyramid.qls.progressReporter.datatype.HighLevelTaskStats</value>
      <value>com.pyramid.qls.progressReporter.datatype.SessionStats</value>
      <value>com.pyramid.qls.progressReporter.datatype.TaskStats</value>
      <value>com.pyramid.qls.progressReporter.datatype.ComputeStats</value>
      <value>com.pyramid.qls.progressReporter.datatype.DetailedInstrumentStats</value>
      <value>com.pyramid.qls.progressReporter.datatype.ImntHistoricalStats</value>
    </list>
  </property>
</bean>

<bean id="QPRXmlView" class="org.springframework.web.servlet.view.xml.MarshallingView">
  <constructor-arg ref="jaxbMarshaller" />
</bean>

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  <property name="mediaTypes">
    <map>
      <entry key="xml" value="application/xml"/>
      <entry key="html" value="text/html"/>
    </map>
  </property>
  <property name="viewResolvers">
    <list>
      <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
      <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
      </bean>
    </list>
  </property>
</bean>

<bean id="QPRController" class="com.pyramid.qls.progressReporter.service.QPRController">
  <property name="jaxb2Mashaller" ref="jaxbMarshaller" />
</bean>

</beans>  

Following is what i am doing in controller (QPRController)

@RequestMapping(value = "/clientMetrics/{clientId}", method = RequestMethod.GET)
public ModelAndView getBatchProgressMetrics(@PathVariable String clientId) {
  List<BatchProgressMetrics> batchProgressMetricsList = null;

  batchProgressMetricsList = batchProgressReporter.getBatchProgressMetricsForClient(clientId);
  ModelAndView mav = new ModelAndView("QPRXmlView", BindingResult.MODEL_KEY_PREFIX + "batchProgressMetrics", batchProgressMetricsList);
  return mav;
}  

And i get the following:

SEVERE: Servlet.service() for servlet rest threw exception
javax.servlet.ServletException: Unable to locate object to be marshalled in model: {org.springframework.validation.BindingResult.batchProgressMetrics=  

Note that BatchProgressMetrics is an interface so my MAV is returning list of BatchProgressMetrics objects and i have entry for its impl in classes to be bound in servlet.xml.

Can you please help me as to what i am doing wrong. And yes if i send just batchProgressMetricsList.get(0) in MAV it just works fine.

© Stack Overflow or respective owner

Related posts about java

Related posts about spring