我是 Spring 服务的新手,我正在尝试创建一个简单的 Web 服务示例,并返回 String Hello Wolrd。我的代码配置是:
<servlet>
<servlet-name>myServletName</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.MyPackageDirectionWebServices.remoting</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyServletName</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</servlet-mapping>
HelloWs.java
package com.MyPackageDirectionWebServices.remoting;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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("/hello")
public class HelloWs {
@RequestMapping(value= "helloWorld", method = RequestMethod.GET)
@ResponseBody
public String HelloWorld() {
return "Hello World";
}
在我的 AppContext-Remoting.xml 中,我添加了跟随注释。
<mvc:annotation-driven />
<context:component-scan base-package="com.MyPackageDirectionServices.remoting" />
但是当我启动我的应用程序并转到服务时:
localhost:8080/MyAppname/rest/hello/helloWorld
我得到 HTTP 404- 并在 Eclipse 控制台中发现了这个错误:
警告:org.springframework.web.servlet.PageNotFound - 在 DispatcherServlet 中找不到带有 URI [/MyAppname/rest/hello/helloWorld] 的 HTTP 请求的映射,名称为“MyServletName”
有人可以帮我吗?非常感谢!