3.1.1中的SpringMVC Restful Service无法运行(在3.0.3中运行正常)

时间:2012-02-23 11:30:08

标签: java web-services spring rest spring-mvc

我对Spring的新版本有一些问题。我做错了什么或忘了任何配置。

具体而言,我正在使用Spring MVC进行RestC服务的PoC。在Spring 3.0.3中正确运行但在Spring 3.1或3.1.1中没有运行。

我的PoC包含:

控制器:

@Controller
public class ProductControllerImpl implements IProductController {

private static Product producto = new Product();

@RequestMapping(value = "/products", method = RequestMethod.GET)
@ResponseBody
public List<Product> getAllProducts() {
    producto.setId(1L);
    List<Product> list = new ArrayList<Product>();
    list.add(producto);
    return list;
}

@RequestMapping(value = "/products/{productId}", method = RequestMethod.GET)
@ResponseBody
public Product getProductById(Long productId) {
    producto.setId(1L);     
    return producto;
}

}

的applicationContext.xml:

<context:component-scan base-package="es.gerardribas.example.controller"/>

<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"/>

<bean id="marshallingHttpMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
    <property name="marshaller" ref="xstreamMarshaller"/>
    <property name="unmarshaller" ref="xstreamMarshaller"/>
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <util:list id="beanList">
            <ref bean="marshallingHttpMessageConverter"/>
        </util:list>
    </property>
</bean>

<mvc:annotation-driven/>

和web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>restservice</display-name>
  <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>restservice</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value></param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>restservice</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

在Spring 3.0.3中运行,但3.1.1中没有运行。

这是控制台日志:

11:44:50,527 DEBUG btpool0-0 servlet.DispatcherServlet:845 - DispatcherServlet with name 'restservice' determining Last-Modified value for [/restservice/products/1]
11:44:50,528 DEBUG btpool0-0 annotation.DefaultAnnotationHandlerMapping:266 - Matching patterns for request [/products/1] are [/products/{productId}]
11:44:50,528 DEBUG btpool0-0 annotation.DefaultAnnotationHandlerMapping:290 - URI Template variables for request [/products/1] are {productId=1}
11:44:50,528 DEBUG btpool0-0 annotation.DefaultAnnotationHandlerMapping:221 - Mapping [/products/1] to handler 'es.gerardribas.example.controller.impl.ProductControllerImpl@2ee7e5'
11:44:50,528 DEBUG btpool0-0 servlet.DispatcherServlet:861 - Last-Modified value for [/restservice/products/1] is: -1
11:44:50,529 DEBUG btpool0-0 servlet.DispatcherServlet:693 - DispatcherServlet with name 'restservice' processing GET request for [/restservice/products/1]
11:44:50,530 DEBUG btpool0-0 support.HandlerMethodInvoker:172 - Invoking request handler method: public es.gerardribas.example.common.domain.Product es.gerardribas.example.controller.impl.ProductControllerImpl.getProductById(java.lang.Long)
11:44:50,531 DEBUG btpool0-0 annotation.AnnotationMethodHandlerAdapter:942 - Written [es.gerardribas.example.common.domain.Product@e634bf] as "application/xhtml+xml" using [org.springframework.http.converter.xml.MarshallingHttpMessageConverter@43b5bb]
11:44:50,531 DEBUG btpool0-0 servlet.DispatcherServlet:808 - Null ModelAndView returned to DispatcherServlet with name 'restservice': assuming HandlerAdapter completed request handling
11:44:50,532 DEBUG btpool0-0 servlet.DispatcherServlet:674 - Successfully completed request

这3.1.1没有运行:

11:43:29,528 DEBUG btpool0-0 servlet.DispatcherServlet:819 - DispatcherServlet with name 'restservice' processing GET request for [/restservice/products/1]
11:43:29,528 DEBUG btpool0-0 annotation.RequestMappingHandlerMapping:213 - Looking up handler method for path /products/1
11:43:29,529 DEBUG btpool0-0 annotation.RequestMappingHandlerMapping:220 - Returning handler method [public es.gerardribas.example.common.domain.Product es.gerardribas.example.controller.impl.ProductControllerImpl.getProductById(java.lang.Long)]
11:43:29,530 DEBUG btpool0-0 support.DefaultListableBeanFactory:245 - Returning cached instance of singleton bean 'productControllerImpl'
11:43:29,530 DEBUG btpool0-0 servlet.DispatcherServlet:902 - Last-Modified value for [/restservice/products/1] is: -1
11:43:29,531 DEBUG btpool0-0 annotation.ExceptionHandlerExceptionResolver:132 - Resolving exception from handler [public es.gerardribas.example.common.domain.Product es.gerardribas.example.controller.impl.ProductControllerImpl.getProductById(java.lang.Long)]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
11:43:29,531 DEBUG btpool0-0 annotation.ResponseStatusExceptionResolver:132 - Resolving exception from handler [public es.gerardribas.example.common.domain.Product es.gerardribas.example.controller.impl.ProductControllerImpl.getProductById(java.lang.Long)]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
11:43:29,531 DEBUG btpool0-0 support.DefaultHandlerExceptionResolver:132 - Resolving exception from handler [public es.gerardribas.example.common.domain.Product es.gerardribas.example.controller.impl.ProductControllerImpl.getProductById(java.lang.Long)]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
11:43:29,532 DEBUG btpool0-0 servlet.DispatcherServlet:957 - Null ModelAndView returned to DispatcherServlet with name 'restservice': assuming HandlerAdapter completed request handling
11:43:29,532 DEBUG btpool0-0 servlet.DispatcherServlet:913 - Successfully completed request

任何人都知道我做错了吗?

非常感谢!

2 个答案:

答案 0 :(得分:2)

在Spring 3.1中,&lt; mvc:annotation-driven&gt; element启用新的带注释的控制器支持类 - 请参阅参考文档中的what's new section。实际上,您的AnnotationMethodHandlerAdapter将被忽略。您可以使用RequestMappingHandlerAdapter替换它,或者考虑使用MVC Java配置,以便更容易地正确定制HandlerAdapter。请参阅参考文档中的configuring Spring MVC部分。

答案 1 :(得分:0)

对我有用的替代方案如下:

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
            <!-- marshaller initialization configuration goes here -->
        </bean>
        <!-- more converters go here -->
    </mvc:message-converters>
</mvc:annotation-driven>