Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting 404 on simple Spring 4 REST service

Tags:

rest

spring

I am trying to access a RESTful web service I have written:

http://localhost:8080/dukegen/ws/family/1

but getting a 404 using the address bar in the browser and do not know why. I am trying to return JSON. I have put Jackson 2 on my classpath:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.3.1</version>
</dependency>

Here is the server output:

Jan 14, 2014 8:29:55 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/ws/family/{familyId}] onto handler 'familyResource'
Jan 14, 2014 8:29:55 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/ws/family/{familyId}.*] onto handler 'familyResource'
Jan 14, 2014 8:29:55 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/ws/family/{familyId}/] onto handler 'familyResource'
Jan 14, 2014 8:29:55 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 360 ms
Jan 14, 2014 8:29:55 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/dukegen/ws/family/1] in DispatcherServlet with name 'dispatcher'

Here is my 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;
         }

}

Here is my dispatcher set up in 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>/ws/*</url-pattern>
  </servlet-mapping>

My mvcContext.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">

    <context:component-scan base-package="ws.hamacher.dukegen.resource"/>

</beans>

Any help would appreciated.

like image 316
badgerduke Avatar asked Jan 15 '14 02:01

badgerduke


People also ask

Why does spring boot say 404 error?

In this quick article, we explained how to debug 404 errors in Spring MVC. We went through the two most common reasons for receiving a 404 response from our Spring application. The first was using an incorrect URI while making the request. The second was mapping the DispatcherServlet to the wrong url-pattern in web.

How do I resolve no mapping?

Make sure you create your package and classes in src/main/java NOT in src/main/resources. If maven doesn't create the src/main/java folder, just create the folder and move the package folders/classes there. Doing this resolved the no mapping error for me. Hope this information will be useful to someone.

What is difference between controller and RestController?

@Controller is used to mark classes as Spring MVC Controller. @RestController annotation is a special controller used in RESTful Web services, and it's the combination of @Controller and @ResponseBody annotation. It is a specialized version of @Component annotation.


1 Answers

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;
       }
   }
like image 77
Anoop Avatar answered Oct 04 '22 00:10

Anoop