【发布时间】:2014-03-10 07:02:58
【问题描述】:
我正在尝试借助 spring mvc 4.0 框架构建一个 hello world restful web 服务。我创建了一个动态 Web 应用程序,在 mythird.attempt.sample.controller 包中添加了 web.xml、rest-servlet.xml 和 MyController.java 文件。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>My sample rest</display-name>
<welcome-file-list>
<welcome-file>Index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
rest-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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="mythird.attempt.sample.controller" />
<mvc:annotation-driven />
</beans>
MyController.java
package mythird.attempt.sample.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.bind.annotation.RestController;
@RestController
@RequestMapping("/myfirst")
public class MyController {
@RequestMapping(value = "/{name}", method = RequestMethod.GET)
public String getGreeting(@PathVariable String name) {
String result="Hello "+name;
return result;
}
@RequestMapping(value="/qq", method = RequestMethod.GET)
@ResponseBody
public String getGreeting1() {
String result="Hello world";
return result;
}
}
我尝试在 apache tomcat 和 glassfish 上部署它。 当我在浏览器中点击 url "localhost:8080//.../myfirst/asdf Glassfish 发出警告,例如 “在具有名称的 DispatcherServlet 中找不到具有 URI [/myfirst/asdf] 的 HTTP 请求的映射。”
并给出了一个包含详细信息的对话框,点击详细信息就可以了
它给了
System.Deployment.Application.InvalidDeploymentException (ManifestParse) - 从 localhost:8080/TestApache1/myfirst/kk 读取清单时出现异常:清单可能无效或无法打开文件。 - 来源:System.Deployment - 堆栈跟踪: 在 System.Deployment.Application.ManifestReader.FromDocument(String localPath, ManifestType manifestType, Uri sourceUri)
请帮帮我!!提前谢谢!!
【问题讨论】: