0

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> 
9
  • How about the configuration? Commented May 11, 2014 at 19:05
  • Could you please post your Spring configuration? Commented May 11, 2014 at 19:06
  • 1
    does your accept header include application/json? Commented May 11, 2014 at 19:06
  • try to put also the log file...when the server starts if u have log4j enabled it should say all the mapped controllers...if you don't see the PersonController listed in that file...well I guess you have to check your spring configrution Commented May 11, 2014 at 19:06
  • Can you reach any other addresses there? Have you tried a leading slash on the mapping? Do you have a content type in your request? Commented May 11, 2014 at 19:06

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.