使用Spring rest API引导弹簧引导和简单的MVC类型应用程序,导致找不到简单GET的404。我搜索了从不正确的web.xml内容到servlet-context.xml条目的响应,我无法找到问题的根源。
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:web="http://java.sun.com/xml/ns/javaee"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WebContent/WEB-INF/spring-servlet.xml</param-value>
</context-param>
<listener>
<listener-
class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<servlet-mapping>
<servlet-name>api</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>defaultHtmlEscape</param-name>
<param-value>true</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
context.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context/
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.restservice.controllers" />
<mvc:annotation-driven />
</beans>
控制器文件
@Controller
@RequestMapping("/")
public class UserController {
@RequestMapping(value = "/greeting", method = RequestMethod.GET)
public @ResponseBody Greeting greeting(@RequestParam(value="name",
defaultValue="World") String name, HttpServletResponse httpResponse_p,
WebRequest request_p) {
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
}
首先tomcat服务器无法启动。如果我删除服务器并重新创建,则重新启动然后服务器启动正常。然后我尝试运行应用程序,服务器无法启动。
关闭应用程序并重新打开确实有效,但404出现了。
答案 0 :(得分:1)
你还有一个&#39; /&#39; ,只需从类级别的@RequestMapping中删除它
@Controller
@RequestMapping("")
public class UserController {