我正在學習Spring框架。我使用REST + JSON創建了SpringMVC的示例項目。我可以能夠成功編譯,但是當我打的REST URL時,系統提示如下消息Spring with REST
org.springframework.web.servlet.PageNotFound noHandlerFound 警告:未找到HTTP請求與URI的映射[/用SpringMVC /在DispatcherServlet中名爲'springmvc'的[rest/employee]。
下面是用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>
而且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>
而且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]"));
}
}
最後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;
}
}
注:我的春天的版本是4.1,我已經在我的類路徑下面罐子加入到輸出轉換成JSON消息
<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>
請幫忙解決這個問題。
由於提前 馬尼萬南
哪個服務器使用的是?它是Tomcat嗎? – developer
是** javaguy **我正在使用tomcat 7 – Mani
Tomcat webapps文件夾中的項目名稱是什麼? – developer