我正在尝试实现简单的网络服务,该服务采用 userId 并在 HTML 网页上显示一些用户评分信息。
我尝试使用 spring mvc 编写一些基于 spring 注解的 web 服务
当我尝试运行以下程序时,出现 404 错误。
http://localhost:8080/EdgeStore/execute/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>
Controller 是
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>
如果这不起作用,请告诉我。我会删除我的答案。
关于java - Spring MVC Web 服务 GET (Rest) 调用 : 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36129756/