【问题标题】:Spring MVC 4.0 RESTFul Web ServicesSpring MVC 4.0 RESTFul Web 服务
【发布时间】: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)

请帮帮我!!提前谢谢!!

【问题讨论】:

    标签: spring rest


    【解决方案1】:

    我认为网址应该是“localhost:8080/{warname}/rest/myfirst/asdf

    web.xml 表示 servlet 将启动所有具有“rest”路径的 url,我认为您在示例尝试中没有使用它

    【讨论】:

    • 我在访问它时使用了战争名称(只是在点中提到了它而不是输入名称).. 没有 web.xml 中的 url 是 /* 所以像你说的那样添加休息对我没有帮助跨度>
    【解决方案2】:

    找到解决方案.. 当我在 Eclipse 中使用普通浏览器而不是嵌入式浏览器时,一切都开始正常了。

    谢谢!

    【讨论】:

    • 你是如何构建这个项目的?
    猜你喜欢
    • 2011-06-13
    • 2017-05-08
    • 2015-02-20
    • 2011-02-09
    • 2015-04-13
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多