Question

I have PersonController as below :

@Controller
@RequestMapping("person")
public class PersonController {

@RequestMapping(value= "/{personId}", method = RequestMethod.GET, produces={"application/json"})
public @ResponseBody Map<String, Object> getPerson(@PathVariable("personId") Integer personId) { 
    // code to get person
}

Tomcat starts up fine, I see this in the console :

Mapped "{[/person/{personId}],methods=[GET],params=[],headers=[] ,consumes=[],produces=[application/json],custom=[]}" onto public java.util.Map<java.lang.String, java.lang.Object> com.test.web.controller.PersonController.getPerson(java.lang.Integer)

But if I hit the url http://localhost:8080/sample/person/1 I get

HTTP Status 404 - /sample/person/1

In the web.xml

<servlet>
  <servlet-name>app</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!--init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/servlet-context.xml</param-value>
  </init-param-->
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>app</servlet-name>
  <url-pattern>/sample/*</url-pattern>
 </servlet-mapping>
Was it helpful?

Solution

I copy/pasted your PersonController class and it worked fine here.

So I did check your web.xml and your app servlet is mapping the pattern "/sample/*".

If I am corret, I suspect your project is called "sample" in Eclipse. In that case, you have to access your site as follows:

http://localhost:8080/sample/sample/person/1

The mapping in your web.xml will always start from your root context, and that is why you are getting 404 error.

If you want to access your controller from the root domain (in this case it is your actual Eclipse project name by default, but it can be configured too) you can use your servlet mapping as follows:

<servlet-mapping>
     <servlet-name>app</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

I recommend that you use /rest/* or other mark since it will scale better for other types of content.

Let me know if it worked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top