简体   繁体   中英

SpringMVC Restful Service in 3.1.1 doesn't run (in 3.0.3 runs ok)

I have some problems with new version of Spring. I am doing something wrong or I forget any configuration.

Concretly, I am doing a PoC of Restful service with Spring MVC. In Spring 3.0.3 runs correctly but in Spring 3.1 or 3.1.1 doesn't run.

My PoC contains:

Controller:

@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/>

And 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>

In Spring 3.0.3 runs but in 3.1.1 doesn't run.

This is the console log:

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

And this 3.1.1 doesn't run:

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

Anyone knows that I am doing wrong?

Thanks a lot!

In Spring 3.1, the <mvc:annotation-driven> element enables the new annotated controller support classes -- see the what's new section in the reference documentation. In effect your AnnotationMethodHandlerAdapter is ignored. You can replace it with RequestMappingHandlerAdapter or alternatively consider using the MVC Java config where it's easier to customize the HandlerAdapter properly. See the section on configuring Spring MVC in the reference docs.

An alternative that worked for me is something like following:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM