Spring Restful Services示例显示错误..!

时间:2014-03-07 05:28:10

标签: java spring rest

我是新手,可以提供全面的服务。我知道我在这方面犯了一些错误。但我无法识别这些。我不知道接下来该怎么办。请建议我以下内容:

  • 我是否需要添加任何库文件?我已经添加了springjars& springcorejars
  • 我在org.springframework.web.bind.annotation个文件中收到错误。
  • 如果可能,请使用Spring Framework为restfull服务提供合适的教程。

我刚刚添加了屏幕截图和下面的代码:

StateController.java

package com.spring.restful;

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
@RequestMapping("/state")
public class StateController {
    @RequestMapping(value = "/{code}", method = RequestMethod.GET)
    public  @ResponseBody String getState(@PathVariable String code) {
        String result;
        if (code.equals("KL")) {
            result = "Kerala";
        } else {
            result = "Default State";
        }
        return result;
    }
}

调度-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <context:component-scan base-package="com.pretech" />
    <mvc:annotation-driven />
  </beans>

的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" 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">
  <display-name>SpringRestFulExample</display-name>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

Screen Shoot of the error page

建议你的想法...... !!!

2 个答案:

答案 0 :(得分:1)

将Spring库复制到WEB-INF / lib中 - &gt;选择所有库 - &gt;右键单击 - &gt;构建路径 - &gt;添加到构建路径 - &gt;它已经完成了

您可以从此链接下载弹簧罐 - &gt; link

有不同的版本,你最好下载版本3及更高版本,因为它们完全支持spring Annotations!

下载完成并解压zip文件后,每个包可能有3个不同的jar文件示例spring core:(lib / bin,source,doc)。你只需要将lib / bin库复制到你的WEB-INF / lib文件夹中!

如果您有任何疑问,请随时提出!

答案 1 :(得分:0)

也发布你的pom.xml。我无法在您的屏幕截图中找到它,但您似乎正在使用Maven构建。 你是否添加了像spring-web等其他必需的罐子?