Getting 404 on simple Spring 4 REST service

Couple of things are not correct here.

First, in your request mapping, the mapping should be consistent. Your class should be mapped to "/ws" and your method which produces the result should be "/family/{familyId}"

In your web.xml you have configured the servlet to respond to /ws/* and your controller is Request Mapped to ws again.This wont work.

Once "/ws/*" is intercepted by your servlet, it should not be repeated in the Request Mappings. The Controller responds to only the URL pattern within its context. Whatever is after "/ws" in your URL is only in the context of the controller.

I generally prefer the servlet to be mapped to "/" and all further resolutions coded within the controller. Just my preference, though.

So the correct configuration is

web.xml

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/mvcContext.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

and the controller

   @Controller
   @RequestMapping("/ws")
   public class FamilyResource {
       @RequestMapping(value="/family/{familyId}", method = RequestMethod.GET, produces="application/json")
       public @ResponseBody Family getFamily(@PathVariable long familyId) {
          .... builds Family object ....
          return family;
       }
   }

Tags:

Rest

Spring