春天和休息

时间:2012-01-12 05:52:50

标签: spring rest

我创建了下面的代码但是我似乎得到以下错误“在我使用”访问服务时,在名为'restspring'的DispatcherServlet中找不到带有URI [/ restspring / service / employees / 1]的HTTP请求的映射“ HTTP://本地主机:8080 / restspring /服务/员工/ 1" 。我错过了什么吗?

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee   /web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
    <servlet-name>restspring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>restspring</servlet-name>
    <url-pattern>/service/*</url-pattern>
</servlet-mapping>
</web-app>

restspring-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
    <value>spring3.rest.bean.Employees</value>
</list>
</property>
</bean>

<bean id="employees" class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg ref="jaxbMarshaller" />
</bean>

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="viewResolvers">
    <list>
        <bean     class="org.springframework.web.servlet.view.BeanNameViewResolver" />
        <bean id="viewResolver"     class="org.springframework.web.servlet.view.UrlBasedViewResolver">
            <property name="viewClass"         value="org.springframework.web.servlet.view.JstlView"/>
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    </list>
</property>
</bean>

employees.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org    /TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<table border=1>
<thead><tr>
    <th>FirstName</th>
    <th>LastName</th>
</tr></thead>
<c:forEach var="employee" items="${employees.employees}">
<tr>
    <td>${employee.firstName}</td>
    <td>${employee.lastName}</td>
</tr>
</c:forEach>
</table>


</body>
</html>

EmployeesController.java

package spring3.rest.controller;
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;
import org.springframework.web.servlet.ModelAndView;

import spring3.rest.bean.Employees;

@Controller
@RequestMapping("/employees")
public class EmployeesController {
  @RequestMapping(method=RequestMethod.GET, value="/{id}")
  public ModelAndView getEmployee(@PathVariable("id") String id){
      Employees e = new Employees();
      e.setFirstName("Eugene");
      e.setLastName("Anthony");
      return new ModelAndView("employees", "employees", e);
  }
}

Employees.java

package spring3.rest.bean;

public class Employees {
private String firstName;
private String lastName;

public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
}

非常感谢您的帮助。

谢谢。

3 个答案:

答案 0 :(得分:0)

生成的war文件的名称是什么?默认情况下,它构成您的上下文根,应该在您的请求网址/service/employees/1之前添加到您的请求中。 <servlet-name>的{​​{1}}只是将请求(由web.xml标识)映射到特定servlet的逻辑名称,它不是URL的一部分。在提出请求时请尝试省略它。

答案 1 :(得分:0)

我没有看到组件扫描来检测您的控制器。如果你错过了它,请把它放进去。样品

<context:component-scan base-package="org.myProject.controller"/>

这会检测@Controller注释,并自动启用&lt; context:annotation-config&gt;处理注释。

查看http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-controller上的链接了解详情。

答案 2 :(得分:0)

我认为你错过了this document中给出的一些步骤。

您需要定义两个bean

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

然后为控制器路径添加组件扫描 前

<context:component-scan base-package="org.springframework.samples.petclinic.web"/>