简体   繁体   中英

Error 404 Spring-MVC

I am trying spring-mvc and json.

My project arch is

src
--main
----java
----resource
----webapp
------WEB-INF
--------web.xml
--------jodoCmsDispatcher-servlet.xml

I have one plain controller

public class CategoryController {   
    @RequestMapping(value = "/kfc", method = RequestMethod.GET)
    public @ResponseBody Shop addCategory(HttpServletResponse response, @PathVariable("parentid") String parentCategoryId, @PathVariable("newcategorytitle") String newcategorytitle )
    {
        Shop shop = new Shop();
        shop.setName("Testing");
        shop.setStaffName(new String[]{"11", "22"});
        return shop;
    }
}

web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB_INF/jodoCmsDispatcher-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>

<servlet>
    <servlet-name>jodoCmsDispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jodoCmsDispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

jodoCmsDispatcher-servlet.xml

.
.
<context:component-scan base-package="com.jodo.cms.controllers" />
<mvc:annotation-driven />

I have tried following url (each time got 404)

localhost - - [05/Apr/2014:11:43:41 +0530] "GET /jodocms/kfc/ HTTP/1.1" 404 949
localhost - - [05/Apr/2014:11:44:20 +0530] "GET /jodocms/kfc HTTP/1.1" 404 949
localhost - - [05/Apr/2014:11:44:24 +0530] "GET /jodocms/ HTTP/1.1" 404 949

Project is deployed successfully in tomcat as shown in localhost:8080/manager

I am trying to sent json data and following " http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/ "
What am i missing ?
Thanks

This is not really needed

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB_INF/jodoCmsDispatcher-servlet.xml</param-value>
</context-param>

As DispatcherServlet by default load the jodoCmsDispatcher-servlet.xml, so remove those lines, that config is only used to add another applicationContext files

And CategoryController must to be marked with @Controller to be scanned

Also consider change

<context:component-scan base-package="com.jodo.cms.controllers" />

to

<context:component-scan base-package="com.jodo.cms" />

Try removing..

@PathVariable("parentid") String parentCategoryId,
@PathVariable("newcategorytitle") String newcategorytitle

Those are causing the conflict as your URL is not correct, so left the method.

  @RequestMapping(value = "/kfc", method = RequestMethod.GET)
    public @ResponseBody Shop addCategory(HttpServletResponse response)
    {
        Shop shop = new Shop();
        shop.setName("Testing");
        shop.setStaffName(new String[]{"11", "22"});
        return shop;
    }

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