0

I have the following configurations: but I am always getting no mapping found error when I hit the api at localhost:9090/springrestexample/employees. It was running when I downloaded and deployed in tomcat. I just updated the package name to my own name as com.exercise.demo (before it was com.howtodoinjava.demo). Similarly, I updated the dispatcher servlet giving my updated package name as <context:component-scan base-package="com.exercise.demo" />

web.xml

<!DOCTYPE web-app PUBLIC  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  "http://java.sun.com/dtd/web-app_2_3.dtd" >   
<web-app>   
    <display-name>Created Web Application</display-name>
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

spring-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.exercise.demo" />
    <mvc:annotation-driven />


</beans>

Controller

@RestController 
public class EmployeeRESTController  {
    @RequestMapping(value = "/employees")
    public @ResponseBody EmployeeListVO getAllEmployees() {
        EmployeeListVO employees = new EmployeeListVO();

        EmployeeVO empOne = new EmployeeVO(1,"Lokesh","Gupta","howtodoinjava@gmail.com");
        EmployeeVO empTwo = new EmployeeVO(2,"Amit","Singhal","asinghal@yahoo.com");
        EmployeeVO empThree = new EmployeeVO(3,"Kirti","Mishra","kmishra@gmail.com");


        employees.getEmployees().add(empOne);
        employees.getEmployees().add(empTwo);
        employees.getEmployees().add(empThree);

        return employees;
    }

    @RequestMapping(value = "/employees/{id}")
    @ResponseBody
    public ResponseEntity<EmployeeVO> getEmployeeById (@PathVariable("id") int id) {
        if (id <= 3) {
            EmployeeVO employee = new EmployeeVO(1,"Lokesh","Gupta","howtodoinjava@gmail.com");
            return new ResponseEntity<EmployeeVO>(employee, HttpStatus.OK);
        }
        return new ResponseEntity(HttpStatus.NOT_FOUND);
    } 
}
Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
  • You have a mapping to `/employees`, but you hit `/springrestexample/employees` - so where have you indicated that your root url is `/springrestexample` ? I can't see it in the posted code – lenach87 May 22 '16 at 17:26
  • EmployeeRESTController is at package com.exercise.demo and I have set at dispatcher servlet. Wouldnt it picked up that controller automatically? Also, I have the following at tomcat server.xml: – user6366573 May 22 '16 at 17:33
  • Yes, you are right, controller should be picked up. I was just asking if you have indicated `/springrestexample` somewhere. Just to be sure - if you hit `localhost:9090/employees` - the error is the same? And is it the same if you change `@RequestMapping(value = "/employees")` to `@RequestMapping(value = "/springrestexample/employees")` ? – lenach87 May 22 '16 at 17:49
  • It is same but i dont think we need to map through root. If i just hit:http://localhost:9090/springrestexample/, it populates Hello World text which I have in index.jsp. App is up and running. Only issue is why the controller is not being picked up – user6366573 May 22 '16 at 17:54
  • Oh, now I see, it really should be the issue with mapping controller then. Do you use IntelliJIdea as IDE? – lenach87 May 22 '16 at 18:08
  • I am using in eclipse Mars – user6366573 May 22 '16 at 18:13
  • Are you sure that your spring-servlet.xml file is recognized as configuration file? Not sure how it is done in eclipse, in IntelliJ you have settings where you can indicate spring configuration - I mean indicate the xml file - or java-based configuration class. Maybe the problem lies in this part... – lenach87 May 22 '16 at 18:28
  • When I deploy the war from tomcat admin console it works fine. But when I deploy the war in eclipse, its not working....its strange – user6366573 May 22 '16 at 18:41
  • You should check if it's the same context path you deployed it from between tomcat and eclipse. Check it in the `server.xml`. – Blank May 23 '16 at 01:53

0 Answers0