Mani
Mani

Reputation: 81

Spring with REST

I am learning Spring framework. I have created a sample project with SpringMVC with REST + JSON. I can able to compile successfully but when i hit the rest url, the system says below message

org.springframework.web.servlet.PageNotFound noHandlerFound WARNING: No mapping found for HTTP request with URI [/SpringMVC/rest/employee] in DispatcherServlet with name 'springmvc'.

Below is the springmvc-servlet.xml

 <context:component-scan base-package="com.springmvc.beans" />
    <context:annotation-config />
    <mvc:annotation-driven />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jsonMessageConverter" />
            </list>
        </property>
    </bean>

<!-- Configure bean to convert JSON to POJO and vice versa -->
<bean id="jsonMessageConverter"
    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean>

And the Web.xml

<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

And the EmployeeController.java

package com.springmvc.beans;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class EmployeeController {

    public static final String GET_EMPLOYEE = "/rest/employee/{empId}";
    public static final String CREATE_EMPLOYEE = "/rest/employee/create/";
    public static final String DELETE_EMPLOYEE = "/rest/employee/delete/{empId}";
    public static final String GET_ALL_EMPLOYEES = "/rest/employee/";

    private Map<String, Employee> employeeData = new HashMap<String, Employee>();
    private Integer count = 1;

    @RequestMapping(method=RequestMethod.GET, value=GET_EMPLOYEE)
    public @ResponseBody Employee getEmployee(@PathVariable ("empId") String empId){
        return employeeData.get(empId);
    }

    @RequestMapping(method=RequestMethod.PUT, value=CREATE_EMPLOYEE)
    public @ResponseBody void createEmployee(){
        employeeData.put(String.valueOf(count++), new Employee(String.valueOf(count++), "Batman"+Math.random(), "23"+Math.random(), "Andartica"+Math.random(), "[email protected]"+Math.random()));
    }

    @RequestMapping(method=RequestMethod.PUT, value=DELETE_EMPLOYEE)
    public @ResponseBody void deleteEmployee(@PathVariable ("empId") String empId){
        employeeData.remove(empId);
    }

    @RequestMapping(method=RequestMethod.GET, value=GET_ALL_EMPLOYEES)
    public @ResponseBody Map<String, Employee> getAllEmployees(){
        return employeeData;
    }


    {
        employeeData.put(String.valueOf(count), new Employee("1", "Mani", "31", "America", "[email protected]"));

    }
}

Finally the Employee.java pojo

package com.springmvc.beans;

/**
 * @author Mani
 *
 */
public class Employee {

    private String empId;
    private String name;
    private String age;
    private String address;
    private String email;

    public String getEmpId() {
        return empId;
    }
    public void setEmpId(String empId) {
        this.empId = empId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Employee(String empId, String name, String age, String address, String email) {
        super();
        this.empId = empId;
        this.name = name;
        this.age = age;
        this.address = address;
        this.email = email;
    }



}

Note : My Spring version is 4.1 and I have added below jars in my classpath to convert output to JSON message

<dependencies>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.1</version>
    </dependency>

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

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.8.1</version>
    </dependency>
</dependencies>

Kindly help to solve this.

Thanks in Advance Manivannan

Upvotes: 0

Views: 51

Answers (1)

Praveen Kumar
Praveen Kumar

Reputation: 1539

looking at the code the endpoint is mapped to

public static final String GET_ALL_EMPLOYEES = "/rest/employee/";

where as the url being accessed is

/SpringMVC/rest/employee

org.springframework.web.servlet.PageNotFound noHandlerFound WARNING: No mapping found for HTTP request with URI [/SpringMVC/rest/employee] in DispatcherServlet with name 'springmvc'.

Try making as below:

GET_ALL_EMPLOYEES = "/rest/employee";

Upvotes: 1

Related Questions