Pergunta

I have tried to create a very simple controller with Spring 4.0.2. It works fine if I used version 3.1.2.RELEASE. However, once I upgraded the maven dependency to 4.0.2.RELEASE. It stop working.

Here is my web.xml

<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>my-app</display-name>
<servlet>
    <servlet-name>springDispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springDispatcher</servlet-name>
    <url-pattern>*.json</url-pattern>
</servlet-mapping>
</web-app>

Here is my springDispatcher-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="viewResolver"  
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>

<context:component-scan base-package="com.tsun.sample"/>
<bean  
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" 
/>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" 
/>
</beans>

Here is my controller

package com.tsun.sample;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class TestController {
    @RequestMapping(value="/test.json", method=RequestMethod.GET)
    public String simpleMethod(ModelMap model) {
        return "hello";
   }
}

Here is my hello.jsp in WEB-INF/jsp

HELLO WORLD!

When I try

http://localhost:8080/my-app/test.json 

There is error 2014-03-14 14:38:34,417 WARN [org.springframework.web.servlet.PageNotFound] (http-0.0.0.0-8080-1) No mapping found for HTTP request with URI [/my-app/test.json] in DispatcherServlet with name 'springDispatcher'

Did I miss anything? It works fine for Spring version 3.1.2 Any feedback is welcome!

Thank you

David

Foi útil?

Solução

Could you first try migrating to Spring 3.2? Using the official migration guide to 3.2 should help you. Also in your case, I think the annotation beans could be replaced by:

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

    <mvc:annotation-driven/>
    <!-- your other beans -->

</beans>

Once done, you can then rely on the guide to Spring 4 migration.

Don't worry, both guides are really lightweight, and separating both tasks should help a lot. Note: check out the Spring 4 dependency update for minimal requirements. It looks like some of the requirements aren't met by Jboss 5.2. Things may work, but won't have access to official support.

Outras dicas

I suspect this is to do with the component scanning not correctly finding resources due to Spring 4 removing the support for the version of the Virtual File System (VFS) used by JBoss 5.2.

Antoine Rey has discussed this (in French) and has developed a fix which is available from Github.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top