[英]Spring MVC Web service GET (Rest) call : 404
我正在尝试实现简单的Web服务,该服务采用userId并在HTML网页上显示一些用户评级的信息。
我试图使用Spring MVC编写一些基于Spring注释的Web服务
当我尝试运行以下程序时,出现404错误。
HTTP://本地主机:8080 / EdgeStore /执行/ USER1
我编写的简单程序。 我在该程序中缺少什么吗?
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<context:component-scan base-package="EdgeStore"/>
<mvc:annotation-driven/>
</beans>
而调度员是
<?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/j2ee"
xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID"
version="2.4">
<display-name>EdgeStore</display-name>
<servlet>
<servlet-name>EdgeStore</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>EdgeStore</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
控制器是
package EdgeStore;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
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.ResponseStatus;
@Controller
@RequestMapping("/execute")
public class executeController {
@RequestMapping(value="/{userId}", method = RequestMethod.GET)
@ResponseBody
public UserProfile getUserProfile(@PathVariable("userId") String userId) {
UserProfile userProfile = new UserProfile();
userProfile.setUserId(userId);
userProfile.setSegmentId("defaultSegement");
return userProfile;
}
}
您能以这种格式更改调度程序并尝试:
<display-name>EdgeStore</display-name>
<servlet>
<servlet-name>EdgeStore</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
// Give location for your config file.
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>EdgeStore</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
如果这不起作用,请告诉我。 我将删除答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.